⚠️ 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 PHP library

2022-04-19 // I proofread this article and updated some links.

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 for getting data from remote APIs, and Providers, which own the logic to extract information.

Adapters

There are many adapters to use Geocoder with, like Buzz, cURL, Guzzle and the 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. It is well integrated with Propel thanks to the GeocodableBehavior and with Doctrine thanks to the DoctrineBehaviors. Both behaviors are really powerful. Install them and your model objects (entities) become geo-aware! For Drupal folks, the geocoder module should use Geocoder sooner or later.

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

If you plan to deal with geocoding stuff in your PHP project, you should give Geocoder a try ;)

Feel free to fork and edit this post if you find a typo, thank you so much! This post is licensed under the Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Recent articles

Comments

No comments here. You can get in touch with me on Mastodon or send me an email if you prefer.