为什么在使用file_get_contents()时出现500错误,但可以在浏览器中使用? [英] Why I'm getting 500 error when using file_get_contents(), but works in a browser?
问题描述
$html = file_get_contents("https://www.[URL].com");
echo $html;
在错误日志中产生此信息:
produces this in the error logs:
PHP警告:file_get_contents(https://www.[URL] .com)[function.file-get-contents]:无法打开流:HTTP请求失败!第13行上的/Applications/MAMP/htdocs/test.php中的HTTP/1.1 500内部服务器错误;
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.
我也尝试使用cURL.我在日志文件中没有任何错误,但是$html
现在回显:
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.
...更多调试信息
有什么办法解决此问题吗?
Any ideas how to work around this?
推荐答案
尝试以下解决方法:
$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n"));
$context = stream_context_create($opts);
$header = file_get_contents('https://www.example.com',false,$context);
如果这不起作用,也许您无法阅读https?
If this doesn't work, maybe you cant read from https?
这篇关于为什么在使用file_get_contents()时出现500错误,但可以在浏览器中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!