Geocoder: The Missing PHP5 LibraryZürich, Switzerland
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:
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:
Guzzle, or Zend Http
If you want to use another HTTP layer, you can easily write your own provider by
The most important part of Geocoder is probably all its providers:
HostIp, IpInfoDB, Yahoo!
and even Geoip, the PHP extension.
Same thing here, you can easily write your own provider by implementing the
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('126.96.36.199'); // Street address based $result = $geocoder->geocode('10 rue Gambetta, Paris, France'); // reverse geocoding $result = $geocoder->reverse($latitude, $longitude);
$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),
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 ;)