2

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?

Charles
  • 4,372
  • 9
  • 41
  • 80

2 Answers2

3

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;
sunshinejr
  • 4,834
  • 2
  • 22
  • 32
  • I've tried something similar to this before, only without using any "cookie file." What exactly was it that made this work, as opposed to what was suggested here that I tried? http://stackoverflow.com/questions/697472/file-get-contents-returning-failed-to-open-stream-http-request-failed – Charles Mar 08 '14 at 23:56
  • That's my standard curl request. I was really looking into it long time ago, and based on a scripts it can be anything, any minor thing and your request is denied by the server. This structure works most of the time. Maybe it was not the best USERAGENT from you, can't tell. – sunshinejr Mar 09 '14 at 00:02
0

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.

Joshua Bixler
  • 531
  • 2
  • 6