1

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.

timbonicus
  • 908
  • 3
  • 13
  • 27

4 Answers4

1

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.

timbonicus
  • 908
  • 3
  • 13
  • 27
  • Had a similar problem, but with https. Found the answer I was looking for here http://stackoverflow.com/questions/1975461/file-get-contents-with-https – Valentin Despa Nov 09 '12 at 11:25
0

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
}
jjclarkson
  • 5,890
  • 6
  • 40
  • 62
  • 1
    Good suggestions, I restarted the web server and added: `ini_set('user_agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');` Sadly, no change. Adding urlencode had no effect either. – timbonicus Aug 17 '09 at 16:51
0

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.

w35l3y
  • 8,613
  • 3
  • 39
  • 51
  • Nothing special, the box is behind a NAT but no proxy. I can use wget from the command line and retrieve the same web pages. – timbonicus Aug 17 '09 at 16:53
  • Have you tried to use file_get_contents on other pages? Like using absolute address to some local file or some other site that doesn't use https. Check if the extension php_openssl.dll was uncommented in your php.ini, save the file and restart the server again. – w35l3y Aug 17 '09 at 17:55
0

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);
}
Daniel
  • 374
  • 2
  • 5