Using YQL to get geo location information for an IP address post

Posted on 2010-07-27 by jwage


The Yahoo! YQL API has the ability to provide geo location information for IP addresses. Using this in PHP is dead simple. Here is a barebones example demonstrating how you can use PHP and YQL for this:

$ipAddress = '76.22.200.69';
$query = sprintf("select * from ip.location where ip='%s'", $ipAddress);
$queryUrl = "http://query.yahooapis.com/v1/public/yql?q=" . urlencode($query)."&format=json&env=".urlencode("store://datatables.org/alltableswithkeys");
$json = file_get_contents($queryUrl);
$data = json_decode($json);
print_r($data);

The above code would output the following:

stdClass Object
(
    [query] => stdClass Object
        (
            [count] => 1
            [created] => 2010-07-27T04:27:20Z
            [lang] => en-US
            [results] => stdClass Object
                (
                    [Response] => stdClass Object
                        (
                            [Ip] => 76.22.200.69
                            [Status] => OK
                            [CountryCode] => US
                            [CountryName] => United States
                            [RegionCode] => 47
                            [RegionName] => Tennessee
                            [City] => Nashville
                            [ZipPostalCode] => 37205
                            [Latitude] => 36.1121
                            [Longitude] => -86.863
                            [Timezone] => 0
                            [Gmtoffset] => 0
                            [Dstoffset] => 0
                        )

                )

        )

)

I took this a step further and built a little abstraction layer on top of this functionality. Now, retrieving geo location information for IP addresses using the YQL API is easier than ever using the PHP YQL Geo Locator library. You can get the code on github. Continue reading to learn how to get started!

First, clone the git repository:

$ git clone git://github.com/jwage/php-yql-geo-locator.git

Now you need to setup your code to use the library:

use GeoLocator\Locator;
use GeoLocator\Location;
use GeoLocator\GoogleMapImage;

require 'php-yql-geo-locator/lib/GeoLocator/Location.php';
require 'php-yql-geo-locator/lib/GeoLocator/Locator.php';
require 'php-yql-geo-locator/lib/GeoLocator/GoogleMapImage.php';

After setting everything up you are ready to start working with geo locations using the locator API:

  • getGeoLocation($ip)
  • getGoogleMapImageForIps(array $ips)
  • getGoogleMapImageForIp($ip)

Here is an example using the getGeoLocation() method:

$geoLocation = $locator->getGeoLocation('76.22.200.69');

It returns an instance of GeoLocator\Location and has a simple public API for retrieving the geo location information for the IP address:

echo $geoLocation->getLatitude();

You can also export all the information to a PHP array using the toArray() method:

print_r($geoLocation->toArray());

It would result in an array that looks like this:

Array
(
    [ip] => 76.22.200.69
    [countryCode] => US
    [countryName] => United States
    [regionCode] => 47
    [regionName] => Tennessee
    [city] => Nashville
    [zipPostalCode] => 37205
    [latitude] => 36.1121
    [longitude] => -86.863
    [timezone] => 0
    [gmtOffset] => 0
    [dstOffset] => 0
)

Get a google map that plots multiple IP addresses:

$image = $locator->getGoogleMapImageForIps(array(
    '76.22.200.69',
    '74.125.65.106'
));

The above method returns an instance of GoogleMapImage and has the following API:

  • setWidth($width)
  • setHeight($height)
  • setMaptype($maptype)
  • setSensor($sensor)
  • setZoom($zoom)
  • addLocation(Location $location)
  • getUrl()
  • getHTMLImageTag()
  • __toString()

Now you can just echo the $image to get the HTML image:

echo $image;

The above would result in an image tag that looks like the following:

<img src="http://maps.google.com/maps/api/staticmap?zoom=&size=550x550&maptype=roadmap&sensor=false&markers=color:blue|label:76.22.200.69|36.1121,-86.863&markers=color:blue|label:74.125.65.106|37.4192,-122.057" width="550" height="550" />

Categories: articles