I have REST API and looks like below :
{
"rajaongkir": {
"query": {
"key": "b5231ee43b8ee75764bd6a289c4c5745"
},
"status": {
"code": 200,
"description": "OK"
},
"results": [
{
"city_id": "1",
"province_id": "21",
"province": "Nanggroe Aceh Darussalam (NAD)",
"type": "Kabupaten",
"city_name": "Aceh Barat",
"postal_code": "23681"
},
{
"city_id": "2",
"province_id": "21",
"province": "Nanggroe Aceh Darussalam (NAD)",
"type": "Kabupaten",
"city_name": "Aceh Barat Daya",
"postal_code": "23764"
}
]
}
}
I want to consume this API. I call the object like code below :
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<title>Raja Ongkir</title>
</head>
<body>
<h1>Raja Ongkir</h1>
<?php
echo var_dump($data->rajaongkir->results[0]);
?>
</body>
</html>
If I call first element of JSON using this line
echo var_dump($data->rajaongkir->results[0]);
OR
echo var_dump($data->rajaongkir->results[0]->city_name);
I got the output that I want. But If I try to get all city_id or city_name in results object, using this code
echo var_dump($data->rajaongkir->results->city_name);
I got this error
Message: Trying to get property 'city_name' of non-object
How to fix it?