最近在 LAMP 服务器上file_get_contents停止读取 http 文件(在同一服务器上用 php 生成),但它读取同一目录中的 html 文件。HTML 文件位于同一服务器上。
任何想法在PHP,Apache或任何其他配置中可能发生了什么变化?
更多详情代码正在读取由CMS生成的一些页面,例如:http://example.com/a-page。从本质上讲,url 是一个配置为使用 .htaccess 命令正确重定向的 http://example.com/index.php?q=a-page
设法找到错误
Error: file_get_contents(http://example.com/site-configuration/footer.html) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
Error type/ Nr.: Warning - 2
File: /var/www/websites/example/assets/snippets/footer/footer-bug.php
Line: 11
Line 11 source: $output = file_get_contents('http://example.com/site-configuration/footer.html');
阿帕奇/2.2.14PHP 版本 5.2.11
谢谢
叫做allow_url_fopen的设置。
如果是任何allow_url_*
设置,您应该会收到一条警告,告诉您有关它的信息。确保您启用了正确的error_reporting
。如果是这种情况,并且您仍然没有得到警告,那么很可能不是这样。
您需要更彻底地调查"停止工作"的实际含义。也许它发出请求,但在响应中收到 HTTP 错误代码?这将使"纯粹"的file_get_contents
回报只是false
.
用
var_dump($http_response_header);
在file_get_contents
调用之后,查看最后一个 HTTP 请求返回的响应。
您还可以将context
传递给 file_get_contents
,以便即使响应标头指示错误,它也会返回响应正文。
$opts = array('http' =>
array(
'ignore_errors' => '1'
)
);
$context = stream_context_create($opts);
file_get_contents($url, false, $context);
它又开始工作了。我被告知这是原木保镖(不确定这是什么)