10
votes

This problem seems to have been discussed in the past everywhere on google and here, but I have yet to find a solution.

A very simple fopen gives me a

PHP Warning: fopen(http://www.google.ca): failed to open stream: HTTP request failed!".

The URL I am fetching have no importance because even when I fetch http://www.google.com it doesnt work. The exact same script works on different server. The one failing is Ubuntu 10.04 and PHP 5.3.2. This is not a problem in my script, it's something different in my server or it might be a bug in PHP.

I have tried using a user_agent in php.ini but no success. My allow_url_fopen is set to On.

If you have any ideas, feel free!

5
Can you do a wget http://www.google.ca from the command line? Is fopen() not giving any more error information?Pekka
can you do fopen('173.194.43.104') ? (that's google.ca's ip) maybe DSN isn't accessible by PHP on that server?Viper_Sb
@Pekka yes, I can fetch no problem this waymickey
@Viper_Sb: no difference DNS is working correctly, and everything else is, I can wget or lynx to the webserver, it's just fopenmickey
that's why I said perhaps it's not accessible by PHP, it could be working just fine but PHP doesn't have access to it.Viper_Sb

5 Answers

10
votes

It sounds like your configuration isn't allowed to use file functions, which is common these days because of security concerns. If you have the cURL libraries available to you, I would recommend trying those out.

PHP: cURL

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.google.ca/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file = curl_exec($ch);
curl_close($ch);

echo $file;
1
votes

Check that your php.ini config is set to allow fopen to open external URL's:

allow_url_fopen "1"

http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

1
votes

I'm not at all sure about whether this is the problem or not, but I know in the past I've had problems with opening URLs with fopen, often due to php.ini's allow_url_fopen or other unknown security settings

You may want to try cURL in PHP, which often works for me, you'll find an example really easily by googling that.

0
votes

Check your phpinfo output - is http present under Registered PHP Streams?

0
votes

Are you getting "HTTP request failed" without further details? The socket timeout could be expired. This default to 60 seconds. See: http://php.net/manual/en/function.socket-set-timeout.php