I am trying to echo a value from google map json url. I can var_dump() the keys and the values but I can't pull the specific value alone. Php code:
<?php
$from = 'Piccadilly+Circus+London+UK';
$to = 'Brighton+Railway+Station+Brighton+UK';
$json ='https://maps.googleapis.com/maps/api/distancematrix/json?origins='.$from.'&destinations='.$to.'&mode=driving&key=APIKEY';
$ch = curl_init($json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
// Print Result
echo '<pre>' . var_dump($data) . '</pre>';
?>
The var_dump() result is as follows;
string(542) "{
"destination_addresses" : [ "Brighton, Queens Rd, Brighton BN1 3XP, UK" ],
"origin_addresses" : [ "Piccadilly Circus, London W1B, UK" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "86.7 km",
"value" : 86665
},
"duration" : {
"text" : "1 hour 59 mins",
"value" : 7114
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
"
I need to assign distance->text to a php variable. Could anyone help?
json_decodeto make that an array.