这是我的请求
$GoogleApiResult = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?origins=11.0450,76.9200&destinations=11.0450,76.9455");
return $GoogleApiResult;
我的回复是
{ "destination_addresses" : [ "201, Koundampalayam, Coimbatore, Tamil Nadu 641030, India" ], "origin_addresses" : [ "1, Luna Plastics Road, Lakshmi Nagar, Coimbatore, Tamil Nadu 641025, India" ], "rows" : [ { "elements" : [ { "distance" : { "text" : "3.5 km", "value" : 3523 }, "duration" : { "text" : "6 mins", "value" : 382 }, "status" : "OK" } ] } ], "status" : "OK" }
如何获得距离(3.5km)和持续时间(6分钟)的值?
答案 0 :(得分:1)
您需要使用json_decode函数将json响应转换为PHP数组。
使用以下代码获取持续时间&与api的响应距离:
$array=json_decode($json); //$json is api response
//var_dump($array);
echo $array->rows[0]->elements[0]->distance->text;
echo $array->rows[0]->elements[0]->duration->text;