【问题标题】:file_get_contents works on localhost but times out on serverfile_get_contents 在本地主机上工作,但在服务器上超时
【发布时间】:2012-11-17 08:18:10
【问题描述】:

我有这个代码:

$feed = 'urltorss';
$feeds =  new SimpleXmlElement( file_get_contents($feed) );


$feed = 'http://www.businessweek.com/feeds/homepage.rss';
$feeds =  new SimpleXmlElement( file_get_contents($feed) );

现在 both url 没有任何特殊字符需要对其进行 url 编码,但第二个 url 有一个 .rss 扩展如果它是相关的。

第一次调用有效,第二次在我的服务器上尝试时超时,但在 localhost 上完美运行。

这是我得到的错误:

    Severity: Warning

Message: file_get_contents('http://www.businessweek.com/feeds/homepage.rss')
[function.file-get-contents]: 
failed to open stream: Connection timed out

Filename: controllers/mailsystem.php

Line Number: 36

这是为什么呢?

【问题讨论】:

  • 很可能是由于您的 PHP 设置。尝试将脚本超时设置为更高的值。
  • 超时是固定超时,在 localhost 中加载速度非常快,如果第一个有效,这怎么可能是设置?
  • 究竟是什么超时?您的脚本或请求?
  • 可以添加网址吗?您可以更改网址中的域名或文件名。
  • 第一个文件在您的服务器中,第二个文件在您的服务器之外,对吧?

标签: php timeout file-get-contents


【解决方案1】:

首先,检查你的路径
echo $feed; // just for debug
然后查看内容
$content = @file_get_contents($feed);
最后,得到 XML
if( $content ) { $feeds = new SimpleXmlElement( file_get_contents($feed) ); }

编辑:URL 的 file_get_contents 仅在您有 allow_url_fopen = 1 时才有效。要获取 http://www.businessweek.com/feeds/homepage.rss 的内容,您需要一个获取内容的 cURL 函数。类似http://davidwalsh.name/curl-download

【讨论】:

  • 这与问题无关,我不想检查错误是否发生,它不应该发生。
  • 这不是无关的......它是关于调试的。关于“我不想检查是否发生错误”......你是程序员吗? :) 因为这就是要知道问题所在。无论如何... URL 的 file_get_contents 仅在您有 allow_url_fopen = 1 时才有效。要获取 businessweek.com/feeds/homepage.rss 的内容,您需要一个获取内容的 cURL 函数。类似davidwalsh.name/curl-download
  • 我的意思是在这一点上我不想那样做......好吧,问题自己解决了,我想这是暂时的......
猜你喜欢
  • 2015-04-09
  • 1970-01-01
  • 1970-01-01
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
相关资源
最近更新 更多