468,771 Members | 1,500 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

fopen() unable to open some URLs?

Hi,

I've used fopen() extensively to open web pages. I've found that there is a
small minority
of web pages that open fine in a browser, but are inaccessible using
fopen(). Here are
two such URLs:

http://www.homes.com/
http://www.dolbyproperties.com/

For example, using the code below, almost every web page that will open in a
browser
will be read correctly by this code. There are few exceptions, such as the
above URLs.
I'm wondering why/how could a page be accessible to a browser but not to
fopen()?

Here's the code:

<?php

$page = "";

if (isset($url))
{
$fp = fopen($url, 'r');

if ($fp != false)
{
while (feof($fp) == false)
{
$page .= fread($fp, 10000);
}

fclose($fp);

echo "URL: $url =====================================<br><br>";
echo $page;
}
}

?>

I have this code running with a simple form interface here:

http://linkmachine.net/fopen_test.php

Any ideas?

Thank you,

-Michael


Aug 11 '05 #1
1 2154
Michael Ferrier wrote:
For example, using the code below, almost every web page that will
open in a browser
will be read correctly by this code. There are few exceptions, such
as the above URLs.
I'm wondering why/how could a page be accessible to a browser but not
to fopen()?


Some websites require an explicit user-agent header in order to return a
response, as is the case with homes.com

There are several ways to include the user agent, of which the easiest is to
apply the ini_set function as follows:

ini_set('user_agent','Mozilla');
$fp = fopen('http://www.homes.com/','r');
fpassthru($fp);

When you run this code, you will see it works fine for homes.com. Bare in
mind that this doesn't work for sites which require more then a user agent
string to operate.

JW

Aug 11 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Shaoyong Wang | last post: by
3 posts views Thread by Ming | last post: by
10 posts views Thread by Longfellow | last post: by
5 posts views Thread by eyalc1978 | last post: by
4 posts views Thread by lucyachammond | last post: by
1 post views Thread by nebelraabe | last post: by
25 posts views Thread by subramanian100in | last post: by
6 posts views Thread by chazzy69 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.