10

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? Commented Jan 5, 2011 at 20:21
  • can you do fopen('173.194.43.104') ? (that's google.ca's ip) maybe DSN isn't accessible by PHP on that server? Commented Jan 5, 2011 at 20:24
  • @Pekka yes, I can fetch no problem this way Commented Jan 5, 2011 at 20:36
  • @Viper_Sb: no difference DNS is working correctly, and everything else is, I can wget or lynx to the webserver, it's just fopen Commented Jan 5, 2011 at 20:36
  • 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. Commented Jan 5, 2011 at 20:44

5 Answers 5

11

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;
Sign up to request clarification or add additional context in comments.

4 Comments

Create a test PHP file with the phpinfo(); line it. Search for "curl" and see if it's enabled.
Ok I see you edited your post, that version is working, i'll try it in my script.
@mickey If this worked for you in your script, go ahead and accept this answer!
still trying to work it out... I'd rather like to find a solution as to why the fopen function is not working, since I have many scripts using this function.
1

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 Comment

this is set to "On" by default in my Ubuntu installation
1

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.

1 Comment

The exact same one? Strange. What does curl_error() give? uk.php.net/manual/en/function.curl-error.php
0

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

1 Comment

https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.