8

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
mOrloff
  • 2,547
  • 4
  • 29
  • 48
  • 2
    `print_r($http_response_header);` might give you a clue as to why it is not responding – Samuel Cook Dec 27 '12 at 19:13
  • 4
    Is the [`allow_url_fopen` setting](http://us3.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen) enabled? – Wiseguy Dec 27 '12 at 19:14
  • does it work if you replace "localhost" with 127.0.0.1? – dmarra Dec 27 '12 at 19:18
  • Grasping at straws here, but try appending /? to your original URL. – dmarra Dec 27 '12 at 19:31
  • What exactly is returned???? Exactly? **Empty** is not a valid PHP value. Is it `FALSE`? Is it really `NULL`? What is it? What does `var_dump` say about it? Why didn't you provide the debug output you gather directly with your question? – hakre Dec 27 '12 at 19:32
  • @mOrloff: Please check `var_dump($http_response_header);` ***after*** you call `file_get_contents` - otherwise it is undefined as you write. That is the next thing to look into. Also very impportant is that you enable error logging and raise the error level to the highest setting to see all notices and warnings. That will tell you more, too. – hakre Dec 27 '12 at 19:50
  • I'm also having a very similar problem to this, only instead of json I'm trying to read a pdf file. Did you ever find a solution? – Novocaine Nov 19 '13 at 11:30

3 Answers3

7

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • XDEBUG is already in the stack. I did try directly echoing an arbitrary string FIRST THING in the target method, and that string was NOT present in the Dump you suggested above. The Dump results are too long for this space, so you can see them [HERE ON PASTEBIN](http://pastebin.com/WczdejpK) – mOrloff Dec 27 '12 at 20:17
  • Do you think Item 9 of that dump, the Refresh to the login page, could be part of it? Also, I'm noticing that the address on that item has a _two_ "index.php"s in a row. What?? Anyhow, I'd expect to be getting SOME sort of error/notice/warning, but ...... – mOrloff Dec 27 '12 at 20:27
  • @mOrloff: You have a good nose! Item 9 looks like the culprit, that is a non-standard redirect not part of the HTTP standard. So PHP does not follow it. See http://stackoverflow.com/questions/283752/refresh-http-header - Also consider to use the [`Location` response header](http://tools.ietf.org/html/rfc2616#section-14.30) instead, that is the correct one. Or use both in case you need to have `Refresh` as well. Also mind the return status code (!), a redirect normally is not 200 but 302 or 301. – hakre Dec 27 '12 at 22:09
2

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!

hattila
  • 490
  • 5
  • 13
1

Check is it enabled in your php.ini

ini_get('allow_url_fopen')
Rush
  • 740
  • 4
  • 13
  • Thank you. That has already been verified as being ON (the earlier comment confirming it has already been deleted to make room for fresher topics). Thanks for asking though. – mOrloff Dec 27 '12 at 19:50