0

i am trying to access a value from a JSON response in PHP.

Here's the JSON output:

JSON Response

{"responseData": {"results":[{"GsearchResultClass":"GimageSearch","width":"1080","height":"720","imageId":"ANd9GcQWSDt_-TnzrNvEGEUoRSi56HfnP6GYXU5nDZYTH-jWw85xAZTOmtjjU5fq","tbWidth":"150","tbHeight":"100","unescapedUrl":"http://jblm-realestateguide.com/wp-content/gallery/fort-lewis-housing-areas_1/tacoma.jpg","url":"http://jblm-realestateguide.com/wp-content/gallery/fort-lewis-housing-areas_1/tacoma.jpg","visibleUrl":"jblm-realestateguide.com","title":"Cities Around Joint Base Lewis McChord, \u003cb\u003eWashington\u003c/b\u003e | Joint Base \u003cb\u003e...\u003c/b\u003e","titleNoFormatting":"Cities Around Joint Base Lewis McChord, Washington | Joint Base ...","originalContextUrl":"http://jblm-realestateguide.com/cities-around-jblm/","content":"\u003cb\u003eTacoma\u003c/b\u003e, \u003cb\u003eWA\u003c/b\u003e","contentNoFormatting":"Tacoma, WA","tbUrl":"http://t3.gstatic.com/images?q\u003dtbn:ANd9GcQWSDt_-TnzrNvEGEUoRSi56HfnP6GYXU5nDZYTH-jWw85xAZTOmtjjU5fq"},{"GsearchResultClass":"GimageSearch","width":"441","height":"441","imageId":"ANd9GcRTxtYjg8d_R9V_fcu9WT2oThkLEzSOBrtnD5yh7sDv3EQ0mc1Ba8GQ4Z4","tbWidth":"127","tbHeight":"127","unescapedUrl":"http://washingtonhottubspacovers.com/imgs/tacoma-wa.jpg","url":"http://washingtonhottubspacovers.com/imgs/tacoma-wa.jpg","visibleUrl":"washingtonhottubspacovers.com","title":"\u003cb\u003eTacoma Washington\u003c/b\u003e Replacement Hot Tub Covers","titleNoFormatting":"Tacoma Washington Replacement Hot Tub Covers","originalContextUrl":"http://washingtonhottubspacovers.com/tacoma-hot-tub-covers.html","content":"\u003cb\u003eTacoma Washington\u003c/b\u003e Replacement","contentNoFormatting":"Tacoma Washington Replacement","tbUrl":"http://t3.gstatic.com/images?q\u003dtbn:ANd9GcRTxtYjg8d_R9V_fcu9WT2oThkLEzSOBrtnD5yh7sDv3EQ0mc1Ba8GQ4Z4"},{"GsearchResultClass":"GimageSearch","width":"800","height":"534","imageId":"ANd9GcTLGrihrETQp8EcOYZdoJtsFRRHOuMEUCQZ3I8fUrhJJw4JX1xOAzvC3Qg","tbWidth":"150","tbHeight":"100","unescapedUrl":"https://lh5.googleusercontent.com/-79aR6CKFGcg/Tc2eit4NyjI/AAAAAAAAAAk/D1L0QcZ4-Aw/w800-h800/Rainier84_mount_rainier_and_tacoma_08-20-84.jpg","url":"https://lh5.googleusercontent.com/-79aR6CKFGcg/Tc2eit4NyjI/AAAAAAAAAAk/D1L0QcZ4-Aw/w800-h800/Rainier84_mount_rainier_and_tacoma_08-20-84.jpg","visibleUrl":"plus.google.com","title":"Ken Williams,CHFC CLTC CLU - Google+","titleNoFormatting":"Ken Williams,CHFC CLTC CLU - Google+","originalContextUrl":"https://plus.google.com/113866325996254808746","content":"Scrapbook photo 3","contentNoFormatting":"Scrapbook photo 3","tbUrl":"http://t0.gstatic.com/images?q\u003dtbn:ANd9GcTLGrihrETQp8EcOYZdoJtsFRRHOuMEUCQZ3I8fUrhJJw4JX1xOAzvC3Qg"},{"GsearchResultClass":"GimageSearch","width":"350","height":"238","imageId":"ANd9GcSmbeFTC2odTglWyJ2hjAuly1HJPNGPNvr2g4BsYy9haSF0aPHg4JN4YxU","tbWidth":"120","tbHeight":"82","unescapedUrl":"http://www.come2tacoma.com/images/tacoma_rainer.jpg","url":"http://www.come2tacoma.com/images/tacoma_rainer.jpg","visibleUrl":"www.come2tacoma.com","title":"\u003cb\u003etacoma\u003c/b\u003e_rainer.jpg","titleNoFormatting":"tacoma_rainer.jpg","originalContextUrl":"http://www.come2tacoma.com/","content":"County \u003cb\u003eWashington\u003c/b\u003e. \u003cb\u003eTacoma\u003c/b\u003e","contentNoFormatting":"County Washington. Tacoma","tbUrl":"http://t1.gstatic.com/images?q\u003dtbn:ANd9GcSmbeFTC2odTglWyJ2hjAuly1HJPNGPNvr2g4BsYy9haSF0aPHg4JN4YxU"}],"cursor":{"resultCount":"5,250,000","pages":[{"start":"0","label":1},{"start":"4","label":2},{"start":"8","label":3},{"start":"12","label":4},{"start":"16","label":5},{"start":"20","label":6},{"start":"24","label":7},{"start":"28","label":8}],"estimatedResultCount":"5250000","currentPageIndex":0,"moreResultsUrl":"http://www.google.com/images?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026imgsz\u003dsmall%7Cmedium%7Clarge%7Cxlarge\u0026imgtype\u003dphoto\u0026hl\u003den\u0026q\u003dtacoma,wa","searchResultTime":"0.17"}}, "responseDetails": null, "responseStatus": 200}

PHP CODE:

$get_location_info = json_decode(file_get_contents($json_url),true);        
$location_image =  $get_location_info->responseData->results[0]->{'unescapedUrl'};

I am trying to access the "unescapedUrl" value from the JSON response and capture it in PHP. I am currently getting a NULL for $location_image in above code.

Any idea what i am doing wrong above? Thanks!

0

4 Answers 4

2

json_decode returns an Array.

To access the unescapedUrl you would need to try something like this

$location_image = $get_location_info['responseData']['results'][0]['unescapedUrl'];

you can also iterate over the results using

foreach($get_location_info['responseData']['results'] as $result){

    echo $result['unescapedUrl'];
}
Sign up to request clarification or add additional context in comments.

3 Comments

It only returns an array if you pass the 2nd param $assoc=true otherwise it defaults to stdClass Objects.
I omitted that because in his question he passes true as the 2nd param
you can pass the 2nd parameter as false
2

Try this

$get_location_info['responseData']['results'][0]['unescapedUrl']

If you weren't using the "true" condition would be something like this

$get_location_info->responseData->results[0]->unescapedUrl

Keep in mind that the "results" bit of this data structure is an array, therefore you should loop through it to get all the "unsescapedUrl" values.

4 Comments

Wont work based on the question which passes true as 2nd param to json_decode.
Nevermind, my mistake, I was seeing it other way, I'll edit this!
I hope you didn't just copy my answer.
RPM, no, I fixed mine, I checked it out again and saw my mistake.
0

You are passing the 2nd param in json_decode, $assoc=true. This returns an associative array instead of the object for the notation you are using.

Update this line to:

$get_location_info = json_decode(file_get_contents($json_url));

without the true param and your object notation will work. And if not do what tf.alves suggested

$location_image =  $get_location_info->responseData->results[0]->unescapedUrl;

Comments

0

Since you are ultimately transforming the JSON into a PHP array, you need to use this syntax:

$count = count($get_location_info['responseData']['results']);
for($i = 0; $i < $count; $i++)
     echo $get_location_info['responseData']['results'][$i]['unescapedUrl'];

Either use an array or object.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.