【问题标题】:no return for file_get_contents()?file_get_contents() 没有回报?
【发布时间】:2011-02-22 06:41:26
【问题描述】:

为什么我在 file_get_contents($myurl) 上没有得到回报,但我确实得到了 wget 的输出

更新:

我使用 curl 获取返回标头和结果,发现它在标头而不是正文中

HTTP/1.1 200 Document follows
Transfer-Encoding: chunked
Content-type: text/plain
Server: XMS (724Solutions HTA XSAM_30_M2_B020 20070803.172831)
Date: Fri, 21 May 2010 10:48:31 GMT
Accept-Ranges: bytes

HTTP/1.1 404 ChargedPartyNotAvailable
Transfer-Encoding: chunked
Content-type: text/plain
Server: XMS (724Solutions HTA XSAM_30_M2_B020 20070803.172831)
Date: Fri, 21 May 2010 10:34:13 GMT
Accept-Ranges: bytes

我怎样才能只提取“200 Document Follow”和“404 ChargedPartyNotAvailable”?

【问题讨论】:

  • 请显示一些代码并更详细地解释你在做什么。

标签: php file-get-contents


【解决方案1】:

您可以使用 PHP Curl 包来检索 URL 的内容

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$myUrl);
$result = curl_exec($ch);
curl_close($ch); 

或者如果您只想使用 file_get_contents,请检查您是否正确配置了 PHP.ini

http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

这里提到 (http://php.net/manual/en/function.file-get-contents.php)

URL 可以用作文件名 这个函数如果 fopen 包装器 已启用。请参阅 fopen() 了解 有关如何指定的更多详细信息 文件名。

【讨论】:

  • PHP 配置正常。它似乎只出现在标题而不是正文
  • 对于那些想要使用变量$result的人,请先在$result = curl_exec($ch)之前添加这个curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);。因为事实上,这个变量是毫无用处的。但对于短卷曲仍然 +1。
【解决方案2】:

您的 php 配置中是否启用了allow_url_fopen?

但是,如果没有,我希望您会收到警告 - 您是否显示错误/警告?您可以在脚本顶部临时添加:

error_reporting(E_ALL);
ini_set('display_errors', true);

然后你可能会明白为什么 file_get_contents() 不起作用。

编辑

如果您只对标头感兴趣,您也许可以使用get_headers()。

【讨论】:

  • +1 for allow_url_fopen 和 error_reporting 建议(尽管我只会记录错误:p)。
  • 配置没问题。结果似乎出现在标题而不是正文中
  • 是的,汤姆,你是对的。我只对标题感兴趣,误以为是 file_get_contents($myurl) 来获取它。
  • @TomHaigh 谢谢。没有错误报告。我遇到了防火墙问题。
【解决方案3】:

你可以试试:

$contents = implode('', file($myurl));

我经常使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多