Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save INDIAN2020/6d3cf01b4af5e2917120531bbdc758b7 to your computer and use it in GitHub Desktop.
Save INDIAN2020/6d3cf01b4af5e2917120531bbdc758b7 to your computer and use it in GitHub Desktop.
How to get lat and lng from Google Maps API with curl #googlemaps
<?php
function yanco_get_lat_long( $address ){
$latlon_array = array();
$google_maps_key = 'XXXXXXXXXX'; // Remember to have a valid API key
$region = 'DK';
$url = 'https://maps.google.com/maps/api/geocode/json?address=' . $address . '&sensor=false&region=' . $region . '&key=' . $google_maps_key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
$lat = $response_a->results[0]->geometry->location->lat;
$long = $response_a->results[0]->geometry->location->lng;
array_push($latlon_array, $lat);
array_push($latlon_array, $long);
return $latlon_array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment