This content has been written a long time ago. As such, it might not reflect my current thoughts anymore. I keep this page online because it might still contain valid information.

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.


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.


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:


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

    new \Geocoder\Provider\YahooProvider($adapter, 'API_KEY'),

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

// 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.


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.


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 ;)

Feel free to fork and edit this post if you found a typo, thank you so much! This post is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.


Sorry, I removed all comments on December 19, 2019. You can tweet me or send me an email if you like.