Charles
Charles

Reputation: 4528

file_get_contents failed to open stream: HTTP request failed only for SPECIFIC web page

I'm getting the following error when attempting perform a file_get_contents on a specific URL: http://lolking.net/champions/. I've had no problems performing file_get_contents on any other web page I've tried.

The error is:

Warning: file_get_contents(http://lolking.net/champions) [function.file-get-contents]: failed to open stream: HTTP request failed!

I think that the page is purposefully blocking the request I'm trying to make. I've also tried using cURL and faking a user agent, but neither of these have worked.

Is there anything else I can do to try to grab information from the aforementioned URL?

Upvotes: 2

Views: 6062

Answers (2)

sunshinejr
sunshinejr

Reputation: 4854

This works for me, remember to have cookies.txt.

$cookie_file = "cookies.txt";
$url = 'http://www.lolking.net/champions';
$c = curl_init($url);
curl_setopt($c, CURLOPT_FRESH_CONNECT, 1);                  
curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);         
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);     
$z = curl_getinfo($c);
$s = curl_exec($c);
curl_close($c);

echo $s;

Upvotes: 3

Joshua Bixler
Joshua Bixler

Reputation: 531

Try

file_get_contents('http://www.lolking.net/champions/');

file_get_contents can't handle HTTP header redirects which this page does to make it go to www.lolking.net. Try it with the last slash on it.

Upvotes: 0

Related Questions