Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么我';使用file_get_contents()时出现500错误,但在浏览器中工作?_Php_File Get Contents - Fatal编程技术网

Php 为什么我';使用file_get_contents()时出现500错误,但在浏览器中工作?

Php 为什么我';使用file_get_contents()时出现500错误,但在浏览器中工作?,php,file-get-contents,Php,File Get Contents,在错误日志中生成以下内容: PHP警告:文件\u获取\u内容(https://www.[URL].com)[function.file get contents]:无法打开流:HTTP请求失败!第13行的/Applications/MAMP/htdocs/test.php中出现HTTP/1.1500内部服务器错误” 但是,该站点在浏览器中运行良好 我也尝试过使用cURL。我在日志文件中没有发现任何错误,但是$html现在回显: “/”应用程序中的服务器错误。 对象引用未设置为对象的实例 …更多调

在错误日志中生成以下内容:

PHP警告:文件\u获取\u内容(https://www.[URL].com)[function.file get contents]:无法打开流:HTTP请求失败!第13行的/Applications/MAMP/htdocs/test.php中出现HTTP/1.1500内部服务器错误”

但是,该站点在浏览器中运行良好

我也尝试过使用cURL。我在日志文件中没有发现任何错误,但是
$html
现在回显:

“/”应用程序中的服务器错误。
对象引用未设置为对象的实例

…更多调试信息

有没有办法解决这个问题?

试试这个解决方法:

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

如果这不起作用,可能您无法读取https?

我必须在标题中输入更多数据:

$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);

您是否考虑过询问其他网站的主机是谁?有趣的解决方案。但是,当我将用户代理更改为Chrome时,我现在得到一个400错误。尽管使用“MyAgent”字符串可以完美地工作。谢谢-它可以工作,但我很好奇为什么会工作。有什么具体的原因吗?@DanSmart PHP的默认用户代理(很可能只是一个空白字符串)被您请求网页的web服务器阻止。这就是为什么您可能需要设置一个假的用户代理。原因是web应用程序在处理用户代理标头时出错。例如,他们的数据库错误地插入了空用户代理。@modu默认情况下有没有办法取消阻止?因为允许在php.in中打开url我不工作,谢谢
$opts = array('http' => array(
    'method' => "GET",
    'header' => "User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0\r\n"
    . "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
    . "Accept-Encoding:gzip, deflate\r\n"
    . "Accept-Language:cs,en-us;q=0.7,en;q=0.3\r\n"
    . "Connection:keep-alive\r\n"
    . "Host:your.domain.com\r\n"
    ));
$context = stream_context_create($opts);
$html = file_get_contents($sap_url, FALSE, $context);