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 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧