I am using the code below to get some data from the json response using the url provided. This is mainly to get lat long from given coordinates for a site I am creating. I am not sure why I am getting
"Trying to get property of non-object"
error but I may have made a mistake as I have not used JSON before
$ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, 'https://api.postcodes.io/postcodes/cm20re?callback=foo'); $result = curl_exec($ch); curl_close($ch); $obj = json_decode($result); echo $result Now I get this echoed on the screen
/**/ typeof foo === 'function' && foo({"status":200,"result":{"postcode":"CM2 0RE","quality":1,"eastings":570635,"northings":206427,"country":"England","nhs_ha":"East of England","longitude":0.469458924538463,"latitude":51.7305869996012,"parliamentary_constituency":"Chelmsford","european_electoral_region":"Eastern","primary_care_trust":"Mid Essex","region":"East of England","lsoa":"Chelmsford 010D","msoa":"Chelmsford 010","incode":"0RE","outcode":"CM2","admin_district":"Chelmsford","parish":"Chelmsford, unparished area","admin_county":"Essex","admin_ward":"Moulsham and Central","ccg":"NHS Mid Essex","nuts":"Heart of Essex","codes":{"admin_district":"E07000070","admin_county":"E10000012","admin_ward":"E05004107","parish":"E43000062","ccg":"E38000106","nuts":"UKH36"}}}); Not sure how to pick a specific value
here is the print value
stdClass Object ( [status] => 200 [result] => stdClass Object ( [postcode] => CM2 0RE [quality] => 1 [eastings] => 570635 [northings] => 206427 [country] => England [nhs_ha] => East of England [longitude] => 0.46945892453846 [latitude] => 51.730586999601 [parliamentary_constituency] => Chelmsford [european_electoral_region] => Eastern [primary_care_trust] => Mid Essex [region] => East of England [lsoa] => Chelmsford 010D [msoa] => Chelmsford 010 [incode] => 0RE [outcode] => CM2 [admin_district] => Chelmsford [parish] => Chelmsford, unparished area [admin_county] => Essex [admin_ward] => Moulsham and Central [ccg] => NHS Mid Essex [nuts] => Heart of Essex [codes] => stdClass Object ( [admin_district] => E07000070 [admin_county] => E10000012 [admin_ward] => E05004107 [parish] => E43000062 [ccg] => E38000106 [nuts] => UKH36 ) ) )