file_get_contents not working on my server but works fine anywhere else?

163 views Asked by At

I am trying to get json data using get_file_contents but I not getting anything back. I tried curl but no avail. Following is my code

$url = 'http://api.icndb.com/jokes/random';
$json = file_get_contents($url);
$obj = json_decode($json,TRUE);
echo "<h2>" . $obj['value']['joke'] . "</h2>";

And tried using curl:

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

$json = get_content('http://api.icndb.com/jokes/random');
$obj = json_decode($json, TRUE);
echo "<h2>" . $obj['value']['joke'] . "</h2>";

Both the code snippets works fine on the PHPfiddle. I checked allow_url_fopen property in php.ini, it is on.

Thanks

0

There are 0 answers