0

Here is my Request

$GoogleApiResult = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?origins=11.0450,76.9200&destinations=11.0450,76.9455");
return $GoogleApiResult;

And my Response is

{ "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" }

How can i get the value of Distance (3.5km) and Duration (6 min) ?

1 Answer 1

1

You need to convert json response to PHP array using json_decode function.

Use below code to get duration & distance from response from 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;
Sign up to request clarification or add additional context in comments.

Comments

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.