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

Work day in London.Micah's new persona is Indian Jones. And he's never even seen the movie! Check out that hat.A very cold walk today. With horses in tow.The real Epping forest.Growing up in the city has taught my boys a few thingsCricket playersIsaac with the car he would love to ownAll aboardPhotoThis guy greeted us on our walk on the common today. He and his sidekick haven't been there before, so when they came trotting round the corner, we Americans got a bit of an adrenaline rush til we figured out he was SUPER friendly!! #englandtrip2017 #eppiSome luck I have these days :stuck_out_tongue_winking_eye:Easter card construction messPre-Easter cookout at the Nicholson's. Quite the crowd!A walk in the forestDate night in Cambridge.

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

Work day in London.Micah's new persona is Indian Jones. And he's never even seen the movie! Check out that hat.A very cold walk today. With horses in tow.The real Epping forest.Growing up in the city has taught my boys a few thingsCricket playersIsaac with the car he would love to ownAll aboardPhotoThis guy greeted us on our walk on the common today. He and his sidekick haven't been there before, so when they came trotting round the corner, we Americans got a bit of an adrenaline rush til we figured out he was SUPER friendly!! #englandtrip2017 #eppiSome luck I have these days :stuck_out_tongue_winking_eye:Easter card construction messPre-Easter cookout at the Nicholson's. Quite the crowd!A walk in the forestDate night in Cambridge.