2

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

3
  • Debug your code. Perhaps it isn't valid JSON. What does $datasearch contain? Commented Sep 2, 2017 at 17:12
  • @ryantxr I assume it must contain an array of JSON data but the page is blank!!! Commented Sep 2, 2017 at 17:55
  • 1
    Never assume. Dump the data and see what it contains. This is what I did with your code to see what it contained. Then I modified your code to get the data you want. Commented Sep 2, 2017 at 18:09

4 Answers 4

5

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);
?>
Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

0

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.