25

Maybe I've been sat here too long staring at this but WHY would file_get_contents return false here? I've cut and paste the URL and it works fine?

$url = "http://jobs.github.com/positions.json?search=" . $as_any . "&location=" .       $location;
// URL contains http://jobs.github.com/positions.json?search=Project Manager&location=London
var_dump($url);
$json = file_get_contents($url);
var_dump($json);
$results = json_decode($json, TRUE);
var_dump($results);
exit;

EDIT:

I have checked for allow_url_fopen and its definitely on.

Don't Panic
  • 13,965
  • 5
  • 32
  • 51
Mrk Fldig
  • 4,244
  • 5
  • 33
  • 64
  • possible duplicate of [Problem in getting contents/ files using file\_get\_contents from url or Problem in reverse geo coding](http://stackoverflow.com/questions/6954220/problem-in-getting-contents-files-using-file-get-contents-from-url-or-problem-i) – mario Feb 10 '13 at 20:01
  • None of the answers there seem to apply to this question. – Barmar Feb 10 '13 at 20:21

5 Answers5

16

Try this:

$query = http_build_query(array('search'=>$as_any, 'location'=>$location));
$url = "http://jobs.github.com/positions.json?" . $query;

The problem is that you weren't URL-encoding the search term, which contains a space. The request was returning a 400 error from the server, which you'd have noticed if you had error reporting enabled.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 3
    In short, I love you..I did try doing $url = urlencode(....) but that didn't seem to work, whats the difference? – Mrk Fldig Feb 10 '13 at 20:35
  • 1
    That will encode all the delimiter characters in the URL, so they won't be recognized as delimiters. – Barmar Feb 10 '13 at 20:37
10

You may need to enable allow_url_fopen in your php.ini

http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

From the documentation:

This option enables the URL-aware fopen wrappers that enable accessing URL object like files.

Your server may be preventing you from opening a file located at a URL using file_get_contents.

Julio
  • 2,261
  • 4
  • 30
  • 56
9

In my case replace file_get_contents() on this simple function

function get_content($URL){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $URL);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
Ruslan Novikov
  • 1,320
  • 15
  • 21
6

Probably allow_url_fopen in your php.ini: http://php.net/manual/en/filesystem.configuration.php

Needs to be allowed.

Emery King
  • 3,550
  • 23
  • 34
1

Sometimes if file_get_contents return false (not found) you should see if the url is encoded.

example:

http://test.net/демо/

Should be:

https://test.net/%D0%B4%D0%B5%D0%BC%D0%BE/

vencedor
  • 663
  • 7
  • 9