Raghbendra Nayak Raghbendra Nayak - 1 year ago 197
PHP Question

Google direction API not returning traffic parameter

I need the distance between two places and also estimated traveling time between them using google direction API.
I am using the documentation of google API.

I am using below code.

//Source address
$a = 'Los Angeles International Airport, 1 World Way, Los Angeles, CA 90045, United States';
//Destination address
$b = 'Beverly Wilshire, Beverly Hills (A Four Seasons Hotel), 9500 Wilshire Boulevard, Beverly Hills, CA 90212, United
//Pass source and destination address in google map API
$url = 'https://maps.googleapis.com/maps/api/directions/xml?origin=' .urlencode($a) . '&destination=' .urlencode($b) . '&travelMode=google.maps.TravelMode.DRIVING&drivingOptions=departureTime:1451302219|trafficModel:google.maps.TrafficModel.BEST_GUESS&key=AIzaSyC7h7m5bRs-BZwk0XTXEQTB74dZujeLzZs';
$data = file_get_contents($url);
$xml = new SimpleXMLElement($data);
echo "<pre>";print_r($xml);echo "<br/>";

I am not recieving the duration_in_traffic data in response($xml).

Answer Source

You must specify the departure_time parameter in the request. I modified your request like what is shown below and manage to get the duration_in_traffic in the response



Response(`duration_in_traffic portion):

<text>24 mins</text>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download