Question

I'm trying to execute a straightforward PHP call to load the contents of a web page: $result = file_get_contents("http://www.google.com");

The result coming back is a strange file not found error: Warning: file_get_contents(http://www.google.com): failed to open stream: No such file or directory in /var/www/html/test.php on line 5

I have "allow_url_fopen = On" on my php.ini and no .htaccess files that might alter the setting in the directory. Any ideas? I've never had trouble with this function before on different servers.

Was it helpful?

Solution

Apparently the HTTP stream wrapper is not present, which causes this error.

print_r(stream_get_wrappers());

Array
(
    [0] => php
    [1] => file
    [2] => data
    [3] => compress.zlib
)

I'm not sure how it was removed or how to restore it, but that would explain it! I've tried stream_wrapper_restore('http') in case it was unregistered somehow, but that has no effect.

OTHER TIPS

I think it is some kind of proxy effect.
Do you use proxy? If this is the case, you must create a stream context with the proxy details.

Did you re-initialize your webserver on changing "allow_url_fopen"?

OR

The user agent "PHP" may be disallowed on the server you are querying.

OR

From the PHP Manual page: Note: If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().

You can do a test of the php.ini settings like this...

if (ini_get('allow_url_fopen') == '1') {
   // use fopen() or file_get_contents()
} else {
   // use curl or your custom function
}

Not sure, but try:

if(($fp=fopen('http://www.google.com/', 'rb'))!=null)
{
 // for php5 and up or use fread for php4
 $contents = stream_get_contents($fp);
 fclose($fp);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top