Skip to content

Instantly share code, notes, and snippets.

@robynitp
Created November 19, 2013 04:10
Show Gist options
  • Save robynitp/7540199 to your computer and use it in GitHub Desktop.
Save robynitp/7540199 to your computer and use it in GitHub Desktop.
Example of accessing the Google Geocoding API and getting results in XML Info here: https://developers.google.com/maps/documentation/geocoding/
<?php
/*
Example of accessing the Google Geocoding API and getting results in XML
Info here: https://developers.google.com/maps/documentation/geocoding/
*/
$base_url = 'http://maps.googleapis.com/maps/api/geocode/';
$format = 'xml'; // 'xml' or 'json'
$address = 'address='.urlencode('350 5th Avenue New York, NY'); // makes the text URL friendly, ie, 350+5th+Avenue+New+York%2C+NY
$url = $base_url.$format.'?'.$address.'&sensor=false'; // Google requires 'sensor=false' parameter
// url should look something like: http://maps.googleapis.com/maps/api/geocode/xml?address=350+5th+Avenue+New+York%2C+NY&sensor=false
// Get the document at this url
$response = file_get_contents($url);
// echo $response; //debug
// Parse XML with SimpleXML in PHP http://www.php.net/manual/en/simplexml.examples-basic.php
$xml = new SimpleXMLElement($response);
//var_dump($xml); // take a look at this to see what data you get back
// For example, you could show the latitude and longitude coordinates
echo "Latitude: ".$xml->result->geometry->location->lat."<br/>\n";
echo "Longitude: ".$xml->result->geometry->location->lng;
@vijaygohil1223
Copy link

vijaygohil1223 commented May 11, 2022

REQUEST_DENIED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment