尝试对特定 URL 执行 file_get_contents
时出现以下错误:http://lolking.net/champions/ 。我在尝试过的任何其他网页上执行 file_get_contents
时都没有遇到任何问题。
错误是:
警告:file_get_contents(http://lolking.net/champions) [function.file-get-contents]:无法打开流:HTTP 请求失败!
我认为该页面有意阻止我尝试发出的请求。我还尝试过使用 cURL 和伪造用户代理,但这些都不起作用。
我还能做些什么来尝试从上述 URL 获取信息吗?
最佳答案
这对我有用,记得有 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;
关于php - file_get_contents 无法打开流 : HTTP request failed only for SPECIFIC web page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22276378/