PHP5 giving failed to open stream: HTTP request failed error when using fopen

mickey picture mickey · Jan 5, 2011 · Viewed 23.1k times · Source

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!

Answer

Michael Irigoyen picture Michael Irigoyen · Jan 5, 2011

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;