Reverse Geocoding in PHP

  • December 24, 2010
  • None

A project I’m working on at the moment had a requirement for reverse geocoding (ie: taking a set of latitude / longitude co-ordinates and returning a placename) of some data coming from an iPhone application.

There’s plenty of information available for doing address to long/lat encoding using google maps and the like, but not much on going the other way.

Luckily, I found the GeoNames database, a freely accessible geographical database which offers an easy to use api.

The specific bit I’m interested in is accessed by calling this url (with your lat/long co-ordinates of course!)

This returns some JSON encoded data as follows:

         "countryName":"United Kingdom",
         "fcodeName":"populated place",

There’s a lot of info there, but the specific bits I was interested in are the placename and the country.

Here’s the code:

      $url = "$lat&lng=$long";
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
      curl_setopt($ch, CURLOPT_PROXY, 'myproxy:800');
      $json = curl_exec($ch);
      $data = json_decode($json, true);
      foreach ($data["geonames"] as $key => $val) {
        $placename = $val["name"];
        $countryname = $val['countryName'];
        $countrycode = $val["countryCode"];

As you can see, I’m sat behind a proxy so I have to use curl to get the page. If you’re not, you can replace all the curl stuff with a simple file_get_contents($url) (depending on your PHP config of course).

Also of note, if you live in the US you can get the nearest address instead of the nearest placename using a different GeoName api call. There’s a list of API calls available here

CPU / GPU temperature gadgets

Just a quick post today, I found these handy desktop gadgets (for windows 7 and probably vista too) and thought I’d share:

The top one, which monitors CPU temperatures is an add on available for CoreTemp. You need to have the coretemp program running for the gadget to work, but the program is fairly unobtrusive and just sits in the taskbar doing it’s thing.

CoreTemp download
CoreTemp add-ons page

The bottom Gadget is monitoring my GPU (graphics card) temperature. Useful if you have an overclocked box and want to keep an eye on your temperatures while gaming. This gadget works for ATI & Nvidia cards, it automagically detects which card you have so there’s no setup needed… just drop it on your desktop. The homepage for this is in German, but if you scroll down a bit you can easily spot the download link

GPU observer gadget

WP Akismet plugin behind proxy

The new HTTP Api in wordpress 2.9 is fantastic, it standardises http requests and allows you to define a proxy through which all http requests are routed.  Useful if, like me, you run a wordpress server on a corporate network.

You can configure your proxy server by adding the following lines to your ‘wp-config.php’

define('WP_PROXY_HOST', '');
define('WP_PROXY_PORT', '800');

However there’s currently one problem with the default installation of wordpress when it comes to proxy support. Akismet (the spam filtering plugin) Doesn’t work behind the proxy. No matter what you put in for your proxy settings you’ll be told ‘unable to connect’.

The reason is that Akismet uses a ‘raw’ socket connection to do its http requests, rather than the spiffy new API. So, here’s how to fix it:

you need to edit the file ‘/wp-content/plugins/akismet/akismet.php’.

search for the function ‘akismet_http_post’ and replace the entire function (you can just rename the old function) with my newly crafted one:

function akismet_http_post($request, $host, $path, $port = 80, $ip=null){
        global $wp_version;
	$akismet_version = constant('AKISMET_VERSION');
        $args = array(
             'user-agent'=>"User-Agent: WordPress/$wp_version | Akismet/$akismet_version",
         $url = "http://".$host.$path;

         if( !class_exists( 'WP_Http' ) )
         include_once( ABSPATH . WPINC. '/class-http.php' );
	 $http_request = new WP_Http;
         $http_response = $http_request->request($url,$args);
         if( is_wp_error( $http_response ) )
         $response[0] = $http_response['headers'];
         $response[1] = $http_response['body'];
	return $response;

And voila, working spam detection behind a proxy.

Joomla Hack: autofill subject line on contact email

So, imagine you have a Joomla site containing lots of pages about…well, anything really. It’d be nice if you could have a ‘request info’ link on each page so users could send you an email with a pre-generated subject line about that page, so you can parse emails (either manually or using some kind of automatic system) coming from the site.

eg: your site has seperate pages about ‘product A’ and ‘product B’. you want a link on ‘product A’s’ page that sends you an email with the subject line ‘ product A enquiry’ and the same for product B.

I can’t find any way of doing this ‘properly’ in Joomla, so here’s a hack I came up with. What it does basically is take a variable from your URL and put it into the subject box. So you can setup a link to ‘ subject’ and the contact email form will automagically have the subject line filled in with ‘test subject’.

First up, setup a contact to send the emails from (you’ve probably done this anyway if you have a ‘contact us’ page).

Now publish that contact using the ‘contact’ view under something like

now here’s the hack:

open up “components/com_contact/views/contact/tmpl/default_form.php”

the line you’re looking for is this one (it’s line 50 in my copy of the file)

change that to this:

The additional line uses the JRequest class to get the value of our ‘subject’ variable. Then we simply write that into the subject field. We also set a default value ‘website enquiry’ so if the subject variable is unset we have a nice default subject line instead of a blank box. using JRequest::getWord to retrieve the variable means our input is automatically filtered to strip out any html / javascript injection attempts.

you can now set up links anywhere in your site to ‘contact-us?subject=your subject’ to automatically fill out the subject line.

JQuery ‘exists’ and ‘checked’ functionality

  • April 20, 2010
  • None

One of the brilliant things about jquery is that it’s so easy to extend, whether that be by writing your own full blown plugin, or with a quick 1 liner to add new functionality.

Here’s a couple that I always find handy. First up, adding an ‘exists’ function, so you can do this:

if ($(selector).exists()){
    //do something here if our selected element exists

the code is really quite simple, basically we return true if the JQuery object returned by our selector has a length greater than 0 (ie: if the selector matches anything), and false if it doesn’t.

jQuery.fn.exists = function(){
          return jQuery(this).length>0;

Next we have something I use all the time, a really simple function that returns true if the checkbox element you call it on is in the ‘checked’ state, and false if it isn’t. So you can do this:

if ('#mycheckbox').checked(){
      //do something here if the checkbox is checked

and the code to add that function is:

jQuery.fn.checked = function(){
         return jQuery(this).is(':checked');

Hopefully those will be of use to someone else, they’re not particularly complex but they can make your code easier to understand and save you a bit of typing.

Produce a ‘patch’ from a subversion repository

I use subversion quite extensively for PHP code versioning (in particular the excellent Redmine software project management tool has a built in subversion server) and recently had a requirement to produce a set of files that had changed from one revision to another. Specifically my repository has some changes committed to it that I don’t want propagating to the live server just yet, but I want some later changes moving across.

Subversion offers excellent branch / tag support, so if I’d thought this through properly I could have created a new branch for the dangerous changes I didn’t want to move to live, then merged any changes from there onwards back into the ‘live’ trunk…basically keeping two seperate branches in the repository.

However, as usual I didn’t think it through and had to hack together a solution to fit the problem.

Here’s the solution, it’s a bash shell script that takes 2 revisions as an argument and produces a directory containing all the files that have changed between those two revisions, maintaining the directory structure. You pretty much end up with a directory that you can drop straight into your live site to update it to whatever revision you like.

For example, my live site is at revision 10. I committed changes at revision 11 that I don’t want going into the live site, but everything from 12 onwards to the current revision of 15 needs patching in. run ‘make patch 12 15’ and then drop the produced directory into your live site.

#simple script to make a very simple patch from a repository
#call with make low high
# where low is the revision your unpatched project is currently at
# and high is the revision you want the patch to go up to
# eg: if your live system is at revision 100 and you want to
# patch it to revision 120 you'd do:
#our subversion url

#first we need to get a list of changes since requested revision 
svn diff -r$1:$2 $url --summarize > filelist.txt

#now remove the repo information from each line, so we're just left with the path from the root upwards
sed -i "s#svn:// #g" filelist.txt
sed -i "s#M  # #g" filelist.txt
sed -i "s#A  # #g" filelist.txt

#now loop through what we've got
#create the relevant folder, and export the file into it
for i in $(cat filelist.txt); 
	filename=$(basename $i);
	path=$(dirname $i);
	mkdir -p patch-r$1/$path 
	svn export --force $url$path/$filename patch-r$1/$path/$filename
	echo "$filename"; 

PHP: clean up your includes!

If you’ve ever worked on any kind of PHP framework, you’ll likely know the pain that a misplaced ‘space’ in one of your includes can cause.  For the rest of you imagine this scenario:

To load a page in my framework, I have an index.php that loads in a ‘core.php’ (which isn’t web accessible for security purposes) which itself then loads in a long (20 or so) list of included files which make up my framework.  So, my core.php might look like this:


which is lovely, keeps all the functionality of the framework nice and segregated.  However, if one of those included files actually outputs anything to the screen, it can cause all sorts of issues, especially if our ‘send_headers’ function is sending our actual HTML headers…. in this case, anything output to the screen before our HTML headers are sent can really cock things up. Even worse, trying to track down where the output is coming from can be a nightmare when you have a large number of included files.

After a bit of thinking I stumbled across the (probably) blindingly obvious way round this without spending hours debugging and trying to track down which included file is the culprit:

good old fashioned output buffering!

by doing this:

// start buffering


//stop buffering, discard buffer contents


we ensure that anything output by the included files is buffered, then discarded before we actually start outputting stuff.


VirtualBox seamless mode

This is my current desktop in the office:

As you can see, KDE4 taskbar on the right, windows 7 taskbar on the left.  Windows from the right can move onto the left screen freely (note the linux terminal on the left screen).  Unfortunately due to limitations in the virtualbox seamless mode windows from the left screen (from windows 7) are limited to that screen.  Also, there’s no fancy aero 3D effects.

Still, it’s a nice way to run windows and Linux side by side.


PHP Javascript / CSS loader

I’m currently working on an in-house management system here at Tyrrell Systems, and employ a lot (seriously, a lot) of javascript for everything from ajax loading of information to various layout tweaks and so on.

When I inherited the project (a good 2 years ago now) the previous coders had made use of various scripts they’d found on the web, as well as a lot of hand crafted stuff.  I started swapping things over to the rather spiffy Jquery library and added a load more functionality.

However, the system has inevitably started to slow down over the last year or so, loading in all those javascript libraries has taken its toll on page load times.  As well as the javascript overhead there’s also around 30 different css files to load in (I like to keep things well segregated!).  This leads to the following problem:

As you can see, it was taking nearly 30 seconds just to load in the css & javascript for a typical page.

So, I started looking for a solution.  Now, the general (and rather obvious) approach to this is to ‘munge’ all your css (or javascript) files into one big file, run that through some kind of optimiser and then serve the resulting file instead.  This reduces page requests & bandwidth used in one go.  This is a good example of this general approach.  Unfortunately, 90% of these scripts rely on using mod_rewrite to redirect calls to css / js files through a PHP script.  My site runs on IIS, so they’re no good for me.  To cut a long story short, I decided to write a class that would:

  • Allow me to merge multiple css / js files into 1.
  • optionally optimise that file before serving it.
  • Cache the output so I only have to do this process when I actually change any of the css / javascript.

This is what I came up with.  It makes use of jsmin-php for javascript minification, and css-tidy for well, css-tidying so you’ll need to have those if you want to use optimisation.  Or you can optionally turn it off.  To use the class:

First initialise it:

$main-css = new cacher("main.css","css");

The first parameter is a filename (can be anything you like) and the second is the type (must be css or js).

Next we add our files:


or we can add every css/js file in a folder like so:


then when we’ve added everything, we output the files:


The output file will be cached (in /theme/cache by default) until you delete the cached copy from that folder. So if you change any of the files included using this class, remember to delete your cached file!

and that’s it.

Just to complete the before / after theme here’s what this did to my loading times:

Here’s the code (as usual, it’s free to use but please give some credit if you do).

nb: the variable $cfg used here is a global class used in the project this came from. Basically, $cfg->root needs to point at the webroot for your project.

class cacher{	    
	function __construct($filename,$type)
	    $this->type = $type;
            $this->filename = $cfg->root."\theme\cache\".$filename;
            $this->url = "/theme/cache/$filename";
            $this->optimise = $optimise;
            if (file_exists($this->filename)) {
                $this->cached = true;
            } else {
                $this->cached = false;
                $this->fh = fopen($this->filename,"a") or die("unable to open file $this->filename");

        function add_folder($newfolder){
            global $cfg;
            if ($this->cached == true){
            } else {
                $dh = opendir($cfg->root.$newfolder);
                while (false !== ($file = readdir($dh))){
                     $ext = substr($file,strrpos($file,'.')+1);
                     if ($this->type == $ext){

        function add_file($newfile){
            if ($this->cached == true){
            } else {

        function output(){
             if ($this->cached == true){
             } else {
             if ($this->type == "css"){
                    if ($this->optimise == true){
                        $css_out = new csstidy();
                        $this->fh = fopen($this->filename,"w");
                    echo "";
             } elseif ($this->type == "js") {
                    if ($this->optimise == true){
                        $js_out = JSMin::minify(file_get_contents($this->filename));
                         $this->fh = fopen($this->filename,"w");
                    echo "";