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 = '';
$query = sprintf("select * from ip.location where ip='%s'", $ipAddress);
$queryUrl = "" . urlencode($query)."&format=json&env=".urlencode("store://");
$json = file_get_contents($queryUrl);
$data = json_decode($json);

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] =>
                            [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://

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

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:


It would result in an array that looks like this:

    [ip] =>
    [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(

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="|label:|36.1121,-86.863&markers=color:blue|label:|37.4192,-122.057" width="550" height="550" />

Categories: articles