Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- fopen() unable to open some URLs?
- Michael Ferrier
August 11, 2005, 7:51 pm
rate this thread
I've used fopen() extensively to open web pages. I've found that there is a
of web pages that open fine in a browser, but are inaccessible using
fopen(). Here are
two such URLs:
For example, using the code below, almost every web page that will open in a
will be read correctly by this code. There are few exceptions, such as the
I'm wondering why/how could a page be accessible to a browser but not to
Here's the code:
$page = "";
$fp = fopen($url, 'r');
if ($fp != false)
while (feof($fp) == false)
$page .= fread($fp, 10000);
echo "URL: $url =====================================<br><br>";
I have this code running with a simple form interface here:
- Janwillem Borleffs
August 11, 2005, 8:30 pm
Re: fopen() unable to open some URLs?
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:
$fp = fopen('http://www.homes.com /','r');
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.