2

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

1 Answer 1

3

You need to fetch data from your json_decode function instead your curl result

So change

$obj = json_decode($result);
echo $result->eastings;// You need to fetch it form $obj instead of $result

To

$obj = json_decode($result);
echo $obj->eastings;// use $obj

Updated

You can get your value using

$obj->result->eastings
Sign up to request clarification or add additional context in comments.

8 Comments

echo $result and post it's value!! OR print_r($obj) and post its value!!
Okay that has echoed the whole json response, no how do I select a particular part?
aslo post print_r($obj) value!!'
I get nothing when I do that
Use $obj->result->eastings
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.