Parviz Khorraminejad
Parviz Khorraminejad

Reputation: 165

GET JSON data using CURL

I have a website which populates it's data through API. I read its documentation to get its data and it suggested using curl. I'm not familiar with curl so I went to learn it by myself and came up with the following code:

$url = 'https://api.flightplandatabase.com/search/plans?fromICAO=EHAM&toName=Kennedy&limit=1';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
$datasearch = json_decode($data);
echo $datasearch['id'];
curl_close($curl);

But I get a blank page while I'm trying to show the plan's id. If I remove curl_setopt then it gives me the whole JSON data. please tell me what I'm doing wrong. Thanks

Upvotes: 2

Views: 11532

Answers (4)

Anis Alibegić
Anis Alibegić

Reputation: 3230

I added the curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json')); line to make sure that we accept data in JSON format.

$datasearch is an array that holds one element. That one element is an array of all properties you want. So, to access those properties, we need to access the element first and then the properties. You do this by $datasearch[0]["id"];. To avoid constantly typing $datasearch[0] you can just reset the $datasearch value to it's first element ($datasearch = $datasearch[0];). Afterwards, you can use it like $datasearch["id"].

<?php
    $url = 'https://api.flightplandatabase.com/search/plans?fromICAO=EHAM&toName=Kennedy&limit=1';
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json'));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($curl);
    $datasearch = json_decode($data, true);

    if(!empty($datasearch)) {
        $datasearch = $datasearch[0];
        echo $datasearch["id"];
    } else {
        echo "Data not fetched.";
    }

    curl_close($curl);
?>

Upvotes: 5

Brian Gottier
Brian Gottier

Reputation: 4582

And you should be doing some error checking, because you could receive a response that has a 500 error or something, if there is a problem at the server:

<?php

$url = 'https://api.flightplandatabase.com/search/plans?fromICAO=EHAM&toName=Kennedy&limit=1';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if($response = curl_exec( $curl ))
{
    if( curl_getinfo( $curl, CURLINFO_HTTP_CODE ) == '200' )
    {
        $json = json_decode($response, TRUE, JSON_PRETTY_PRINT);

        echo $json[0]['id'];
    }
    else
    {
        $curl_info = curl_getinfo( $curl );
    }
}
else
{
    echo 'Error';
}
curl_close($curl);

Upvotes: 0

ryantxr
ryantxr

Reputation: 4219

The way you are reading the data does not match the actual data. After decoding you have an array of objects. The array contains one array element. Therefore, you have to use $datasearch[0] to get the first element and then ->id to the the id element from the object.

$url = 'https://api.flightplandatabase.com/search/plans?fromICAO=EHAM&toName=Kennedy&limit=1';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
$datasearch = json_decode($data);
if ( $datasearch ) {
    echo $datasearch[0]->id;
}
else {
    echo "bad data";
}
curl_close($curl);

Upvotes: 0

Gabriele Carbonai
Gabriele Carbonai

Reputation: 459

Your code is correct, just an error how you try to take the json's object.

change like this and will be work

echo $datasearch[0]->id;

Upvotes: 1

Related Questions