Fixing escaping problems in WP-Syntax

I've installed the WP-Syntax wordpress plugin for code highlighting, and am very impressed, except for a nasty bug that was causing code snippets to escape html special characters.

Thankfully Gergely Hodicska has a solution on his blog.

Hopefully this sees integration into the actual plugin's codebase at some point.

PHP Geocoding tutorial with the Google Maps API - Part One

So unless you live under a non-Web 2.0-enabled rock, you've probably heard of the magic of the Google maps API and google maps mashups. If you're saavy, you've probably even heard that now Google Maps offer translation of addresses into latitude and longitude, aka Geocoding.

What you may not know is that the folks at google exposed this geocoding as a regular old URL-based webservice, which means that whatever Nifty mashups your devious little minds can come up with (as long as they fly with google's TOS) can be powered on the backend without ever loading a google map! <!-- more --> So lets cut the chit-chat and jump to some code... I'm going to use PHP here, but do whatever fuels your nerding.

First, sign up for a key, put in whatever URL you happen to own, it won't matter (i'll explain later).

next, add this to your php file, making SURE to replace the key with your own:

<?
//Three parts to the querystring: q is address, output is the format, key is the GAPI key
$key = "YOUR KEY HERE";
$address = urlencode("columbia MO");
 
//If you want an extended data set, change the output to "xml" instead of csv
$url = "http://maps.google.com/maps/geo?q=".$address."&output=csv&key=".$key;
//Set up a CURL request, telling it not to spit back headers, and to throw out a user agent.
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER,0); //Change this to a 1 to return headers
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
$data = curl_exec($ch);
curl_close($ch);
 
echo "Data: ". $data;
?>

Supposing all goes well you should see the following: (if not check the PHP CURL manual)

Data: 200,4,38.951667,-92.333889

You'll notice that the first number is the return code, the second the relative accuracy (there 4 we see here is quite low as a result of the broad input. 8 is street-level address accuracy), third the latitude, and fourth the longitude.

So now that we've seen the magic.. let's get it all nicely formatted:

<?
//Check our Response code to ensure success
if (substr($data,0,3) == "200")
{
$data = explode(",",$data);
 
$precision = $data[1];
$latitude = $data[2];
$longitude = $data[3];
 
} else {
echo "Error in geocoding! Http error ".substr($data,0,3);
}
?>

And that's it! Less than 40 lines of code to give you a powerful resource for mapping and usability. Just how powerful? You'll have to wait for part two to find out...

*Note on locations and API keys: In my experience thus far, the Geocoding service doesn't actually check that the api key is being called from the originating server. This may change in the future at some point, so keep your eyes open. FULL CODE LISTING:

<?
//Set up our variables
$longitude = "";
$latitude = "";
$precision = "";
 
//Three parts to the querystring: q is address, output is the format (
$key = "YOUR KEY HERE";
$address = urlencode("columbia MO");
$url = "http://maps.google.com/maps/geo?q=".$address."&amp;output=csv&amp;key=".$key;
 
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
$data = curl_exec($ch);
curl_close($ch);
 
echo "Data: ". $data."&lt;br&gt;";
//Check our Response code to ensure success
if (substr($data,0,3) == "200")
{
$data = explode(",",$data);
 
$precision = $data[1];
$latitude = $data[2];
$longitude = $data[3];
 
echo "Latitude: ".$latitude."&lt;br&gt;";
 
echo "Longitude: ".$longitude."&lt;br&gt;";
 
} else {
echo "Error in geocoding! Http error ".substr($data,0,3);
}
 
?>

Update:

A few people have mentioned that after some number of requests (between 100-300) in quick succession google will kill your access for the day, so you may want to insert a

sleep(1);

To stagger the requests a little.