xml - how to parse JSON google maps JSON in php -


how parse json google maps json in php

i have in xml, coordinates driving direction's not complete json, how can coordinate driving json in php?

if using xml

$xml = simplexml_load_file('http://maps.googleapis.com/maps/api/directions/xml?origin='.$origin.'&destination='.$dest.'&sensor=false'); $startlat = $xml->xpath("/directionsresponse/route/leg/step/start_location/lat"); $startlng = $xml->xpath("/directionsresponse/route/leg/step/start_location/lng"); $distanceeach = $xml->xpath("/directionsresponse/route/leg/step/distance/value"); $distance = $xml->xpath("/directionsresponse/route/leg/distance/value"); 

but how json? , want variable $startlat, $startlng, etc, contains same value if xml

you can retrieve data in json format, , use json_decode() function build stdclass object, can recursively walk through object's property convert (array) $response this.

and use google maps api response array. can proceed no converting array, , use object of stdclass this:

$response->directionsresponse->route->leg->step->start_location->lat; 

to correct values can var_dump() response object, , see want get.

hope, helps.


here code usage google json response google translate api... same, need:

if (!is_null($json = json_decode(file_get_contents($url))) { return $json->data->translations[0]->translatedtext; } 

that's it...


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -