Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Run this as http://example.com/test.php
- */
- function do_it( $where ) {
- $params = array(
- 'origin' => 'New York, NY',
- 'destination' => $where,
- 'sensor' => 'true',
- 'units' => 'metric'
- );
- // Join parameters into URL string
- foreach($params as $var => $val){
- $params_string .= '&' . $var . '=' . urlencode($val);
- }
- // Request URL
- $url = "http://maps.googleapis.com/maps/api/directions/json?".ltrim($params_string, '&');
- // Make our API request
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $return = curl_exec($curl);
- curl_close($curl);
- // Parse the JSON response
- $directions = json_decode($return);
- // Print distance
- printf(
- '<p><strong>Distance from %s: </strong>%s</p>',
- $where,
- $directions->routes[0]->legs[0]->distance->text
- );
- }
- do_it( 'Manchester, NH' );
- do_it( 'Philadelphia, NJ' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement