简体   繁体   中英

PHP file_get_contents returning false

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.

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.

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.

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

Needs to be allowed.

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/

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM