0

I am using the following in php to return the driving distance details between 2 locations:

$url = 'http://maps.google.com/maps/nav?q=from:London%20to:Dover';
$data = @file_get_contents($url);

A JSON object is returned, i am not familiar with JSON can somebody please demonstrate how I can get the "Meters" element from this object into a string.

Thanks in advance.

4 Answers 4

5

Use json_decode:

$obj = json_decode($data);
echo $obj->Directions->Distance->meters;
Sign up to request clarification or add additional context in comments.

1 Comment

@brux You should know that the property name is "meters" with a lowercase "m".
1

You might want to look at the json_decode() function...

$url = 'http://maps.google.com/maps/nav?q=from:London%20to:Dover';
$data = json_decode(@file_get_contents($url));

echo $data->Directions->Distance->meters." meters";

Result:

123561 meters

Comments

1

Use json_decode. JSON Decode

$url = 'http://maps.google.com/maps/nav?q=from:London%20to:Dover';
$data = @file_get_contents($url);
$obj = json_decode($data);
print $obj->meters;

Comments

1

Try this online json Viewer you can call JSON.parse in javascript to parse the string php output to a javascript object.

var json = {}; // parsed json object

var meters = json.Directions.Distance.meters; // is '123561'

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.