sim.plified.com

Chris Pollock

Chris Pollock - web developer & ecommerce entrepreneur
undivided… my thoughts on world, family, church, business, technology and Jesus Christ (all in all)

Get Location Information via visitor IP with Yahoo Query Language (YQL) and PHP

I was reading a Read/Write Web article and YQL and started to monkey around with it a bit today.  I was in need of an script that returned information on the users location based upon their IP.  In a few minutes I was able to pull together a quick script to start parsing out information based on a user’s IP.  Nothing fancy here, but it works!  There are some limits to how many times you can query YQL (read the documentation for more information).

    $ip = $_SERVER['REMOTE_ADDR']; 

    $url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20ip.location%20where%20ip%3D'{$ip}'&format=xml&env=http%3A%2F%2Fdatatables.org%2Falltables.env";
    $xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);

    // if you need to see what is coming back.. uncomment below.
    // print_r($xml);

    // store the information you want using xpath

    $country =  $xml->xpath("/query/results/Response/CountryCode");
    $city =  $xml->xpath("/query/results/Response/City");
    $lat =  $xml->xpath("/query/results/Response/Latitude");
    $long =  $xml->xpath("/query/results/Response/Longitude");

    echo "<p>" . $country[0];
    echo "<p>" . $city[0];
    echo "<p>" . $lat[0];
    echo "<p>" . $long[0];

2 Comments, Comment or Ping

  1. Thanks Nice post i got the proper solution

  2. Hamid

    http://developer.yahoo.com/yql/console/?q=select%20*%20from%20pidgets.geoip%20where%20ip%3D%27128.100.100.128%27&env=http%3A%2F%2Fdatatables.org%2Falltables.env

Reply to “Get Location Information via visitor IP with Yahoo Query Language (YQL) and PHP”

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Things I see

IMG_7347BookFinally!Little peanutTime to set sailEyes openSleepy TimeAri Jeremiah PollockBig brotherBaby's HomeBike manBiker gangBirthday monopoly.$5 StarIMG_2881

Chris Pollock

Web Developer - proficient in both PHP and ASP.NET.
Rochester, New York

View my web developement site.

View Chris Pollock's LinkedIn profile

My Pictures

IMG_7347BookFinally!Little peanutTime to set sailEyes openSleepy TimeAri Jeremiah PollockBig brotherBaby's HomeBike manBiker gangBirthday monopoly.$5 StarIMG_2881