Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP file_get_contents returning false

Tags:

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.

like image 540
Mrk Fldig Avatar asked Feb 10 '13 19:02

Mrk Fldig


People also ask

What will the file_get_contents () return?

The function returns the read data or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false .

What is the use of file_get_contents in PHP?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

Which is faster cURL or file_get_contents?

file_get_contents() is slightly faster than cURL.

What is the difference between file_get_contents () function and file () function?

The file_get_contents() function reads entire file into a string. The file() function reads the entire file in a array, whereas file_get_contents() function reads the entire file into a string.


1 Answers

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.

like image 189
Barmar Avatar answered Sep 19 '22 16:09

Barmar