douchungu0859 2014-03-08 23:20
浏览 100
已采纳

file_get_contents无法打开流:HTTP请求仅针对SPECIFIC网页失败

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?

  • 写回答

2条回答 默认 最新

  • duanfei1930 2014-03-08 23:45
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!