-3

I'm using an API which returns a JSON. here is the link to JSON http://getpincodes.info/api.php?pincode=421305 this is oupput json of above link [{"pincode":"421305","city":"Vidyashram","district":"Thane","state":"MAHARASHTRA"}] here is my code to read data:

  $json = file_get_contents("http://getpincodes.info/aenter code herepi.php?pincode=$zip");
$data=json_decode($json);
$city = $data->city;
echo $city; 
enter code here

i m getting the error while doing so Notice: Trying to get property of non-object in

Prashant
  • 17
  • 6

1 Answers1

0

You seem to be receiving an array of results instead of a naked object. So

$city = $data->city; // does not work: $data is an array

but

$city = $data[0]->city; // should work: $data[0] is an object with a city key
tucuxi
  • 17,561
  • 2
  • 43
  • 74