Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: "file_get_contents()" returning NULL from content-verified URL

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
like image 515
mOrloff Avatar asked Dec 27 '12 19:12

mOrloff


3 Answers

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.

like image 90
hakre Avatar answered Sep 18 '22 00:09

hakre


I can see this is an old post, but I had a similar issue today with file_get_contents() using a relative file path, returning an empty string.

I use UTF-8 encoding on all my files, and I found that I forgot to change the encoding on one file (which I couldn't retrive via file_get_contents() ). When I changed the encoding it "magically" worked.

Thre problem was coused by the non ANSI characters in an ANSI encoded file (eg.: á,é,ű,ő).

Hope this helps someone!

like image 22
hattila Avatar answered Sep 19 '22 00:09

hattila


Check is it enabled in your php.ini

ini_get('allow_url_fopen')
like image 38
Rush Avatar answered Sep 19 '22 00:09

Rush