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];
  • http://www.solutions4ever.net Web Developer

    Thanks Nice post i got the proper solution

  • Hamid
  • http://www.w3scripts.net Baheer

    Hello,
    I just got this fatal error, I just copied these lines and pasted into my location.php file, which I access through the localhost.

    ==============
    Warning: simplexml_load_file(http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20ip.location%20where%20ip%3D'::1'&format=xml&env=http%3A%2F%2Fdatatables.org%2Falltables.env) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /Applications/XAMPP/xamppfiles/htdocs/facebook/location.php on line 4

    Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20ip.location%20where%20ip%3D'::1'&format=xml&env=http%3A%2F%2Fdatatables.org%2Falltables.env" in /Applications/XAMPP/xamppfiles/htdocs/facebook/location.php on line 4

    Fatal error: Call to a member function xpath() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/facebook/location.php on line 11
    =========

  • http://www.turkishpropertiesabroad.com Yusuf Tokgoz

    fantastic tutorial that is what i was looking for my new counter script

Things I see

#gocubs. I’ll root for them over the Dodgers :)PhotoBig stuff planned here tomorrow night. #alcsIt’s hot in these here parts.Hunting is one of the few activities that necessitates slowing down and becoming keenly aware of your surroundings.Magic hour #nofilterHunting time. #youthhuntWarming up his swing. GO #southsidelittleleague !!My first ever granny square! Why when I tried this years ago could I not figure it out? Probably YouTube tutorials dawned since then! Like a free craft class at 9:30 at night --- hard to beat it:blush:The house is set for next weekend. #youthhunt Mr SmilesHappy birthday facesNo wonder Lucas has been eating like a horse lately... he's growing like a weed!!! #lifewithboys #growthspurt #threeyearoldsarefunDecided to try and remember how to crochet. Top is my first attempt at a square :joy:. Oops!!! #droppingstitches #crochetingisfunHanging out w mom and Julie in the finger lakes for the weekend. Had a fun time at Naples Grape festival. #lifewithgirls #umbrellahatsarecool #naplesgrapefest

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

#gocubs. I’ll root for them over the Dodgers :)PhotoBig stuff planned here tomorrow night. #alcsIt’s hot in these here parts.Hunting is one of the few activities that necessitates slowing down and becoming keenly aware of your surroundings.Magic hour #nofilterHunting time. #youthhuntWarming up his swing. GO #southsidelittleleague !!My first ever granny square! Why when I tried this years ago could I not figure it out? Probably YouTube tutorials dawned since then! Like a free craft class at 9:30 at night --- hard to beat it:blush:The house is set for next weekend. #youthhunt Mr SmilesHappy birthday facesNo wonder Lucas has been eating like a horse lately... he's growing like a weed!!! #lifewithboys #growthspurt #threeyearoldsarefunDecided to try and remember how to crochet. Top is my first attempt at a square :joy:. Oops!!! #droppingstitches #crochetingisfunHanging out w mom and Julie in the finger lakes for the weekend. Had a fun time at Naples Grape festival. #lifewithgirls #umbrellahatsarecool #naplesgrapefest