【问题标题】:PHP: "file_get_contents()" returning NULL from content-verified URLPHP:“file_get_contents()”从内容验证 URL 返回 NULL
【发布时间】:2012-12-13 03:40:15
【问题描述】:

我打赌它最终会变得很简单......但我可以用一只手来发现它。

问题:
当我尝试通过file_get_contents() 获取特定本地 URL 的内容时,它以空字符串 '' (length=0) 的形式返回。

我的尝试:

    $url = 'http://localhost/bbq/index.php/company/get/id/2131/return/json';
    $results = file_get_contents($url);
    echo'<pre>Results:<br />',var_export($results),'</pre>';

当直接访问http://localhost/bbq/index.php/company/get/id/2131/return/json时,一切都美妙地回响:{"id":"2131","value":"Acme Anvil Corp."}

为了检查自己,我尝试了 $url = 'http://www.google.com',它按预期工作,所以我尝试了一个单独的本地 uri,http://localhost/?phpinfo=1,它也工作了 > 很好。

问题:
我错过了什么???


[附加信息]
allow_url_fopen 已开启

可能刚刚发现一些东西
作为 CODEIGNITER 项目中的一项安全措施,控制器脚本的第一行有:

    if ( !defined('BASEPATH')) exit('No direct script access allowed');

将其注释掉并没有带来任何改进,但我认为仍然值得一提。

此外,这里是 HTTP 响应标头的转储。也许有人可以看到那里的东西。这一切对我来说似乎很简单,但我真的不知道我应该寻找什么。

array (size=13)
  0 => string 'HTTP/1.1 200 OK' (length=15)
  1 => string 'Date: Thu, 27 Dec 2012 19:58:47 GMT' (length=35)
  2 => string 'Server: Apache/2.4.2 (Win64) PHP/5.4.3' (length=38)
  3 => string 'X-Powered-By: PHP/5.4.3' (length=23)
  4 => string 'Set-Cookie: csrf_cookie_name=4ae46f9a7e612ae22a080a4e88bd349c; expires=Thu, 27-Dec-2012 21:58:47 GMT; path=/' (length=108)
  5 => string 'Set-Cookie: PHPSESSID=ah70p0ldl5qptcauei1t8ihud3; path=/' (length=56)
  6 => string 'Expires: Thu, 19 Nov 1981 08:52:00 GMT' (length=38)
  7 => string 'Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0' (length=77)
  8 => string 'Pragma: no-cache' (length=16)
  9 => string 'Refresh: 0;url=http://localhost/bbq/index.php/index.php/user/login' (length=66)
  10 => string 'Content-Length: 0' (length=17)
  11 => string 'Connection: close' (length=17)
  12 => string 'Content-Type: text/html' (length=23) 

[/附加信息]

顺便说一句,我正在跑步:

  • Win7 64 上的 WAMPSERVER
  • Apache 2.4.2
  • PHP 5.4.3
  • CodeIgniter 2.1.2

【问题讨论】:

  • print_r($http_response_header); 可能会告诉您为什么它没有响应
  • allow_url_fopen setting 是否启用?
  • 把“localhost”换成127.0.0.1能用吗?
  • 在这里抓住稻草,但尝试附加 /?到您的原始网址。
  • 究竟返回了什么???确切地? Empty 不是有效的 PHP 值。是FALSE吗?真的是NULL吗?它是什么? var_dump 对此有何评论?你为什么不直接提供你收集的调试输出和你的问题?

标签: php codeigniter httpresponse file-get-contents


【解决方案1】:

现在开始调试:

# we want text output for debugging. Either this works or you need to look into
# source in your browser (or even better, debug with the `curl` commandline client.)
header('Content-Type: text/plain');

# For quick and dirty debugging - start to tell about if something is worth
# to warn or notice about. (do logging in production)
error_reporting(~0); ini_set('display_errors', 1);

$url = 'http://localhost/bbq/company/get/id/2131/return/json';
$results = file_get_contents($url);
var_dump($http_response_header, $results);

# hard exit, flush all (potential) buffers.
die();

由于var_dump,对文件的请求现在必须至少返回一些文本输出。您还可以在标头调用下方的开头添加一些标记输出,因此很明显该脚本已被调用并且它确实开始运行了。

但是,更好的变体是使用步进调试器并创建带有断点的调试会话。我推荐 Xdebug。

【讨论】:

  • XDEBUG 已经在堆栈中。我确实尝试在目标方法中直接回显任意字符串 FIRST THING,并且您上面建议的 Dump 中不存在该字符串。 Dump 结果对于这个空间来说太长了,所以你可以看到它们HERE ON PASTEBIN
  • 你认为那个转储的第 9 项,刷新到登录页面,可能是其中的一部分吗?另外,我注意到该项目上的地址连续有一个 two “index.php”。什么??无论如何,我希望得到某种错误/通知/警告,但是......
  • @mOrloff:你的鼻子很好!第 9 项看起来像是罪魁祸首,这是一个非标准重定向,不是 HTTP 标准的一部分。所以 PHP 不遵循它。请参阅stackoverflow.com/questions/283752/refresh-http-header - 也可以考虑改用Location response header,这是正确的。或者同时使用两者以防您还需要Refresh。还要注意返回状态码 (!),重定向通常不是 200,而是 302 或 301。
【解决方案2】:

我可以看到这是一篇旧帖子,但我今天遇到了类似的问题,file_get_contents() 使用相对文件路径,返回一个空字符串。

我对所有文件都使用 UTF-8 编码,但我发现我忘记更改一个文件的编码(我无法通过 file_get_contents() 检索)。当我更改编码时,它“神奇地”起作用了。

问题是由 ANSI 编码文件中的非 ANSI 字符引起的(例如:á,é,ű,ő)。

希望这对某人有所帮助!

【讨论】:

    【解决方案3】:

    检查它是否在您的 php.ini 中启用

    ini_get('allow_url_fopen')
    

    【讨论】:

    • 谢谢。这已经被验证为开启(之前的评论确认它已经被删除,以便为更新的主题腾出空间)。谢谢你的提问。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    相关资源
    最近更新 更多