【问题标题】:Getting 404 error while using get_headers for my other site为我的其他站点使用 get_headers 时出现 404 错误
【发布时间】:2016-09-27 04:07:26
【问题描述】:

我遇到了一个问题,我想不通。 我想使用 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 错误的原因。谢谢

【问题讨论】:

  • 它对我有用。我得到 200 好的响应。可能是权限/php.ini 的东西。你能发布你的 phpinfo();输出?
  • 这里是phpinfo theblueish.com/phpinfo.php

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


【解决方案1】:

请参阅为了做到这一点,您必须在 php.ini 中设置 allow_url_fopen = On;

【讨论】:

  • 我运行脚本的两个站点和另一个站点都设置为 ON
  • @ADoe,在你运行脚本的服务器上设置这个。
【解决方案2】:

我认为重定向存在问题 (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);

【讨论】:

  • 仍然出现 404 错误 Url is: http://theblueish.com/file.txt HTTP/1.1 404 Not Found Date: Sun, 29 May 2016 06:41:08 GMT Server: Apache Content-Length: 325 Content-Type: text/html; charset=iso-8859-1 Not Found The requested URL /file.txt was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. 1
  • 如果我在其他服务器上运行相同的脚本,我得到 200 OK 但在我正在工作的服务器上运行 404
  • 嗯,问题出在 DNS 中。请出示您的 hosts 文件以作为开始。或显示ping theblueish.com 结果。
  • 我解决了这个问题。这是 cpanel 上的防火墙,现在它正在工作,不允许连接到该站点
猜你喜欢
  • 2018-09-30
  • 2019-11-05
  • 2014-02-15
  • 2015-01-08
  • 2018-06-07
  • 2020-12-11
  • 2011-10-18
  • 1970-01-01
  • 2022-01-02
相关资源
最近更新 更多