reegan29
reegan29

Reputation: 930

Get the data from json url using php?

This is a json api : https://jobs.github.com/positions.json?description=java&page=1 I want get the data from this url.

<?php 
$url = file_get_contents('https://jobs.github.com/positions.json?description=java&page=1');
var_dump(json_decode($url,true));   ?>

this code return null I also check the url in json validator: http://jsonformatter.curiousconcept.com/ the json is valid but i con't able get the data from this url please help me...

Upvotes: 2

Views: 356

Answers (1)

Kevin
Kevin

Reputation: 1240

Try this script to determine what the problem is. If there is no JSON module installed (see @julian comment), you can try to use PHP implementations of JSON like this: http://pear.php.net/pepr/pepr-proposal-show.php?id=198

if (! extension_loaded('json')) {
    echo 'Module JSON not available!';
    exit();
}

$url = file_get_contents('https://jobs.github.com/positions.json?description=java&page=1');
$data = json_decode($url,true);

switch (json_last_error()) {
    case JSON_ERROR_NONE:
        echo ' - No errors';
    break;
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
    break;
    case JSON_ERROR_STATE_MISMATCH:
        echo ' - Underflow or the modes mismatch';
    break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' - Unexpected control character found';
    break;
    case JSON_ERROR_SYNTAX:
        echo ' - Syntax error, malformed JSON';
    break;
    case JSON_ERROR_UTF8:
        echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
    break;
    default:
        echo ' - Unknown error';
    break;
}

Upvotes: 2

Related Questions