且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP:“file_get_contents()”从内容验证的URL返回NULL

更新时间:2023-02-26 13:40:34

立即开始调试:

 #我们希望文本输出用于调试。或者这是工作,或者你需要在浏览器中查找
#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.