Geocoder: The Missing PHP5 Library

Seven months ago, I released Geocoder, a PHP 5.3 library to ease geocoding manipulations. More and more applications have to deal with geolocation, and even if HTML5 provides a Geolocation API, a server side library is always useful. Today this library has more than 230 watchers on GitHub, and it's time to introduce it here.

The library is standalone, and split in two parts: HttpAdapters which are responsible to get data from remote APIs, and Providers which own the logic to extract information.

Adapters

There are many adapters to use Geocoder with: Buzz, cURL, Guzzle, or Zend Http Client. If you want to use another HTTP layer, you can easily write your own provider by implementing the HttpAdapterInterface interface.

Providers

The most important part of Geocoder is probably all its providers: FreeGeoIp, HostIp, IpInfoDB, Yahoo! PlaceFinder, Google Maps, Bing Maps, OpenStreetMaps, CloudMade, and even Geoip, the PHP extension. Same thing here, you can easily write your own provider by implementing the ProviderInterface interface.

Geocoder supports both geocoding and reverse geocoding. It depends on the provider you choose, and also what you want to do. The API is really simple:

<?php

$geocoder = new \Geocoder\Geocoder();
$adapter  = new \Geocoder\HttpAdapter\BuzzHttpAdapter();

$geocoder->registerProviders(array(
    new \Geocoder\Provider\YahooProvider($adapter, 'API_KEY'),
));

// IP based
$result = $geocoder->geocode('88.188.221.14');

// Street address based
$result = $geocoder->geocode('10 rue Gambetta, Paris, France');

// reverse geocoding
$result = $geocoder->reverse($latitude, $longitude);

The $result variable is an instance of ResultInterface. Again, the API is simple.

Dumpers

Another feature provided by Geocoder is the ability to dump a ResultInterface object in standard formats like GPS eXchange (GPX), GeoJSON, Keyhole Markup Language (KML), Well-Known Binary (WKB), or Well-Known Text (WKT). This is too small to become a separated library, and it can be helpful if you need to share geolocated data.

Conclusion

Geocoder is quite stable now, and is well integrated with Propel thanks to the GeocodableBehavior, and even with Doctrine2 thanks to the DoctrineBehaviors. Both behaviors are really powerful, install them, and your model objects (entities) will become geo-aware. Note for Drupal guys, the geocoder module should use Geocoder, sooner or later.

Geocoder has more than ten contributors (thank you so much guys), is actively maintained, and already used in production! Oh, and it's heavily unit tested with more than a hundred tests, and almost a thousand assertions.

If you plan to use geocoding stuff in your project, or to integrate something in your favourite Framework, you should give Geocoder a try ;)

Links

By the way, if you found a typo, please fork and edit this post. Thank you so much!
This post is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

If you like this post, or if you use one of my Open Source projects, don't hesitate to send me an email. Oh, and just in case, here is my Amazon Wish List. Thank you ♥