Skip to content

Instantly share code, notes, and snippets.

@jakemcgraw
Created October 2, 2011 02:03
Show Gist options
  • Save jakemcgraw/1256943 to your computer and use it in GitHub Desktop.
Save jakemcgraw/1256943 to your computer and use it in GitHub Desktop.
DOTGO Geocoding Engine
<?php
header("Content-type: text/xml");
if (empty($_POST) || !isset($_POST["sys_query"])) {
echo "<message><content>Invalid request. Try again.<br/>Reply:<br/><a query=\"crbspt loc\" /> Geocode (1 address)</content></message>";
exit;
}
$address = preg_replace('/^(crbspt\s+loc|loc\s+|1\s+)/', '', trim(strtolower($_POST["sys_query"])));
$url = sprintf("http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false", urlencode($address));
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
file_put_contents("result.txt", $result);
if (CURLE_OK == ($curl_err = curl_errno($ch))) {
$data = json_decode($result);
if ($data->status === "OK") {
$entry = $data->results[0];
$lat = htmlspecialchars($entry->geometry->location->lat);
$lng = htmlspecialchars($entry->geometry->location->lng);
$xml = <<<XML
<block>
<set name="lat">$lat</set>
<set name="lng">$lng</set>
<message>
<content>OK! We've got your location:<br/>
<br/>
Lat: $lat<br/>
Lng: $lng</content>
</message>
</block>
XML;
echo $xml;
}
else {
echo "<message><content>Invalid response ($curl_err) from Google! $url</content></message>";
}
}
else {
echo "<message><content>Invalid request to Google? $url</content></message>";
}
curl_close($ch);
<?xml version="1.0"?>
<cmrl>
<match pattern="">
<message>
<content>Welcome!<br/>
<br/>
Reply:<br/>
<a query="crbspt loc" /> Geocode (1 address)<br/>
<a query="crbspt cur" /> Current location<br />
</content>
</message>
</match>
<match pattern="loc">
<match pattern="*">
<engine href="http://crbspt.com/geocode.php" />
</match>
</match>
<match pattern="cur">
<message>
<content>Your current location:<br /><br />Lat: <get name="lat"/><br />Lng: <get name="lng" /></content>
</message>
</match>
</cmrl>
@jakemcgraw
Copy link
Author

Send text message "crbspt" to DOTCOM (368266) to demo.

Replace all instances of "crbspt" with your own domain keyword (foobar for foobar.com).

Google Geocoder allows up to 2,500 requests per day.

Enjoy!

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