简体   繁体   中英

Json returns null value

I want to get Json data from Carquery API. But none of my code worked.

First i tried to get the data using curl. This is my code

    $loginUrl = 'http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$loginUrl);
    $result=curl_exec($ch);
    curl_close($ch);
    var_dump(json_decode($result));

It returned null. And then i tried using file_get_content. This is my code :

$json = file_get_contents('http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015');
$obj = json_decode($json);
var_dump($obj);

Unfortunately it didn't work either.

Any help please?

First of all try this:

    $loginUrl = 'http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$loginUrl);
    $result=curl_exec($ch);
if($result === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    var_dump(json_decode($result));
}
    curl_close($ch);

so you can see error if any on curl request.

and 2nd thing that url: 'http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015'

are you sure that the must be 2 (two) ? (question marks) ?

json data return from that API is not correct, i tried using jsonlint.com to check it.

If the callback part is removed from

' http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015 ';

into

' http://www.carqueryapi.com/api/0.3/?cmd=getTrims&year=2015 ';

it will produce right json and can be parsed either using file_get_content() or curl.

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