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 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?