PHP:“file_get_contents()”从内容验证的URL返回NULL [英] PHP: "file_get_contents()" returning NULL from content-verified URL

查看:201
本文介绍了PHP:“file_get_contents()”从内容验证的URL返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



问题:

/ strong>

当我尝试通过 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>结果:< br />',var_export($ results),'< / pre>

直接访问 http://localhost/bbq/index.php/公司/ get / id / 2131 / return / json ,一切都很美好: {id:2131,value:Acme Anvil Corp。 / code>



为了检查自己,我用 $ url ='http://www.google.com' / code>,并且它按预期工作,然后我尝试单独的本地uri, http:// localhost /?phpinfo = 1 问题:

我是什么?缺少






[ADDITIONAL INFO] >
allow_url_fopen 是ON

>某些。

作为CODEIGNITER项目的安全措施,控制器脚本的第一行具有:

  if(!defined('BASEPATH'))exit('不允许直接脚本访问); 

评论它没有提供任何改进,但我认为这可能值得一提。 / p>

此外,这里是一个HTTP响应头的转储。也许有人可以看到那里的东西。这一切看起来都很直接,但我真的不知道我应该寻找什么。

  array = 13)
0 => string'HTTP / 1.1 200 OK'(length = 15)
1 => string'Date:Thu,27 Dec 2012 19:58:47 GMT'(length = 35)
2 =>字符串'服务器:Apache / 2.4.2(Win64)PHP / 5.4.3'(长度= 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)



[/ ADDITIONAL INFO]



BTW :我正在运行:




  • Win7 64上的WAMPSERVER

  • Apache 2.4.2

  • PHP 5.4.3

  • CodeIgniter 2.1.2


解决方案

立即开始调试:

 #我们希望文本输出用于调试。或者这是工作,或者你需要在浏览器中查找
#source(或者更好,用`curl`命令行客户端调试)
header('Content-Type:text / plain');

#对于快速和脏的调试 - 开始告诉是否值得
#警告或通知。 (do log 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);

#硬退出,刷新所有(潜在)缓冲区。
die();

现在,对文件的请求必须至少返回一些文本输出,因为 var_dump 。您还可以在标题调用之下的开头添加一些标记输出,因此很清楚,该脚本已被调用,并且已开始运行。



但是更好的变体正在使用步骤调试器并创建具有断点的调试会话。我推荐Xdebug。


I'm betting that it'll end up being something simple ... but I can use a hand in spotting it.

Problem:
When I try to get the contents of a specific local URL via file_get_contents(), it comes back as an empty string '' (length=0).

What I've tried:

    $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>';

When directly visiting http://localhost/bbq/index.php/company/get/id/2131/return/json, everything echos beautifully: {"id":"2131","value":"Acme Anvil Corp."}

To check myself, I tried it with $url = 'http://www.google.com', and it worked as expected so then I tried a separate Local uri, http://localhost/?phpinfo=1, and it also worked just fine.

Question:
What am I missing???


[ADDITIONAL INFO]
allow_url_fopen is ON

I may have just found something.
As a safety measure in CODEIGNITER projects, the first line of the controller script has:

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

Commenting it out didn't provide any improvements, but I thought it may be worth mentioning nonetheless.

Also, here's a dump of the HTTP Response Header. Maybe someone can see something in there. It all seems pretty straight forward to me, but I don't really know what I should be looking for.

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) 

[/ADDITIONAL INFO]

BTW, I'm running:

  • WAMPSERVER on Win7 64
  • Apache 2.4.2
  • PHP 5.4.3
  • CodeIgniter 2.1.2

解决方案

Start to debug this now:

# 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();

A request to the file must now at least give you back some text output because of the var_dump. You can also add some marker output at the beginning below the header call, so it's clear that the script has been called and it did start to run.

However a better variant is using a step-debugger and creating a debugging session with breakpoints. I recommend Xdebug.

这篇关于PHP:“file_get_contents()”从内容验证的URL返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆