douzhang5121 2012-05-09 22:03
浏览 878
已采纳

为什么我在使用file_get_contents()时遇到500错误,但在浏览器中有效?

$html = file_get_contents("https://www.[URL].com"); 
echo $html;

produces this in the error logs:

PHP Warning: file_get_contents(https://www.[URL].com) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in /Applications/MAMP/htdocs/test.php on line 13";

However, the site works fine in a browser.

I tried using cURL as well. I don't get any errors in the log file, but $html now echoes:

Server Error in '/' Application.
Object reference not set to an instance of an object.

...some more debugging info

Any ideas how to work around this?

  • 写回答

2条回答 默认 最新

  • dongqiao3927 2012-05-09 22:08
    关注

    Try this workaround:

    $opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0
    "));
    $context = stream_context_create($opts);
    $header = file_get_contents('https://www.example.com',false,$context);
    

    If this doesn't work, maybe you cant read from https?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统
  • ¥15 快手联盟怎么快速的跑出建立模型