Get coordinates of an address in PHP using the Google API

This is just a quick dump of the code we used, you may need to heavily modify it.

$address = str_replace(' ', '+', trim($dealer_info['street'])).'+'.str_replace(' ', '+', trim($dealer_info['city'])).'+'.str_replace(' ', '+', trim($dealer_info['state']));
$url = "$address&sensor=falseĀ®ion=UnitedStates";
$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);
$response_a = json_decode($response);
queryDB("UPDATE dealers SET `lat`='".$response_a->results[0]->geometry->location->lat."', `long`='".$response_a->results[0]->geometry->location->lng."' WHERE id='".$dealer_info['id']."' LIMIT 1", 'e');

Shane Stebner

Author: Shane Stebner

Shane is a successful web-developer specializing in responsive design, and the open-source LAMP stack. Over the years, he's gained in-depth experience with Stripe, Twilio, PhantomJs,, PayPal, Facebook, Twitter, and many other plugins/APIs.

comments powered by Disqus