在我的其他网站上使用get_headers时出现404错误

时间:2016-05-29 06:16:51

标签: php apache curl http-headers http-status-code-404

我遇到了一个问题,我无法弄明白。 我想使用file_get_contents或curl从我的其他站点获取数据,我首先验证该文件是否存在于另一台服务器上,但我收到404错误,即使文件存在于服务器上,该文件也不存在。

$fileBlue = 'http://theblueish.com/file.txt';  // File on my other server
echo 'Url is: '.$fileBlue.'<br>';
$file_headers = get_headers($fileBlue); 
print_r($file_headers); 

输出

Url is: http://theblueish.com/file.txt
Array ( [0] => HTTP/1.1 404 Not Found [1] => Date: Sun, 29 May 2016 06:13:38 GMT [2] => Server: Apache [3] => Content-Length: 325 [4] => Connection: close [5] => Content-Type: text/html; charset=iso-8859-1 ) 

当我尝试在线HTTP标头检查站点时,它返回200 OK http://www.webconfs.com/http-header-check.php

请帮助我解决为什么服务器给我404错误的问题。感谢

2 个答案:

答案 0 :(得分:0)

请参阅为了执行此操作,您必须在php.ini中设置allow_url_fopen = On;

答案 1 :(得分:0)

我认为重定向问题(304)。我建议您使用curl,如下所示:

$fileBlue = 'http://theblueish.com/file.txt';  // File on my other server
$ch = curl_init($fileBlue);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,CURLOPT_HEADER,true);
echo 'Url is: '.$fileBlue.'<br>';
$file_headers = curl_exec($ch);
curl_close($ch);
print_r($file_headers);