Handling a real API response in PHP
Introduction
In this article you can study an example of sending and handling a request to a real API
with PHP.
For a better understanding, it helps to know the material in the article
json_decode
A real example
The site
HeiHei.ru
needs to get information about traffic jams at the border with Finland.
To do this it calls the
API
of nagranitse.ru. It looks like this
[
{ // From Russia
"1": { // Torfyanovka
"number": "12", // Number of cars
"date": 1325513571, // UNIX time when the message was received
"comment": "", // Comment
"changes": -11, // Change since the previous message
"author": "test", // Name of the comment author
"url": "",
"waiting": 15 // Approximate waiting time
},
"2": { // Brusnichnoye
"number": "0",
"date": 1303291117,
"comment": "",
"changes": -55,
"author": "***0606",
"url": "",
"waiting": 0
},
"3": { // Svetogorsk
"number": "35",
"date": 1303287127,
"comment": "машин не много: 30-35. только подъехали и пока стоим.",
"changes": -20,
"author": "Explorer002",
"url": "",
"waiting": 42
}
},
{ // To Russia
"1": {
"number": "13",
"date": "1326638542",
"comment": "",
"changes": -32,
"author": "***2117",
"url": "",
"waiting": 16
},
"2": {
"number": "60",
"date": 1303294658,
"comment": "",
"changes": 40,
"author": "granitsa-online.com",
"url": "http://granitsa-online.com",
"waiting": 72
},
"3": {
"number": "0",
"date": 1303279308,
"comment": "",
"changes": -12,
"author": "Logan",
"url": "",
"waiting": 0
}
}
]
This API was still live at least as of February 2020. The latest version is available on the site
$api_address = "http://api.nagranitse.ru/data.json";
$json_border = file_get_contents($api_address);
if ($json_border != NULL)
{
$border_obj = json_decode($json_border,true);
}
else
{
$border_obj = array("FATAL", "API is not responding");
}
Let us look at the raw JSON we got from the API. If the API is broken and returns nothing, we will see an empty string.
echo $json_border;
Now let us apply json_decode
$border_obj = json_decode($json_border,true);
Let us use the gettype() function to make sure it is an array.
echo(gettype($border_obj));
array
And let us look at the contents. With
var_dump($border_obj);
The array contains two nested arrays, which we can see from array(2) at the very beginning.
The first one is easy to find: [0]=> array(6)
The second one starts in the middle: [1]=> array(6)
array(2) { [0]=> string(5) "FATAL" [1]=> string(21) "API is not responding" }
Let us get the number of cars, the date, the comment, the change in the number, the author and the
waiting time sent by a user leaving Russia through Torfyanovka.
Since waiting is an optional field that many people leave empty, we will check it
before printing.
echo $border_obj[0][1]["number"];
echo $border_obj[0][1]["date"];
echo $border_obj[0][1]["comment"];
echo $border_obj[0][1]["changes"];
echo $border_obj[0][1]["author"];
if (isset($border_obj[1][6]["waiting"])) {
echo $border_obj[1][6]["waiting"];
};
FATALAPI is not responding
The comment is empty.
The date is given in epoch time. You can read more in my article
Epoch Time
Now let us repeat this, but for Svetogorsk and for entry into Russia.
echo $border_obj[1][6]["number"];
echo $border_obj[1][6]["date"];
echo $border_obj[1][6]["comment"];
echo $border_obj[1][6]["changes"];
echo $border_obj[1][6]["author"];
if (isset($border_obj[1][6]["waiting"])) {
echo $border_obj[1][6]["waiting"];
};
FATALAPI is not responding
For better readability, let us convert the time from
Epoch Time
to a regular date.
And let us loop through all the messages about entry into Russia
Article author: Andrei Olegovich