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

There’s the verse that says “love covers a multitude of sins”, well... so does snow:grimacing::flushed::stuck_out_tongue_closed_eyes:. #mudseason #nograssagain #lifewithboysMicah’s been working on his beard. It’s filling in quite nicely!! #mountainman #lifewithboys #11yearoldswithbeardsSick day for Lucas and therefore the rest of us. It’s nice though to have the windows open!!What a difference a haircut makes! Spock to Simon. #beforeandafter #spockhair #haircut #shearingmysheepCelebrating Chinese New Year with a friend (a little early) with some treats straight from Beijing! Yummy!This greeted me this morning. :heart_eyes: #lifewithboys #ivalentinesdayPhotoApparently my read aloud book was riveting to this guy:yum:. The rest of us enjoyed it though. A historical fiction account of Tyndale’s life by Scott O’Dell. #thehawkthatcouldnothuntbyday #readaloud #historicalfictionPhotoThe current favorite face of the house! It’s a cute one!! #puppylifeYour future pilot :man:‍:airplane:Agamemnon, Menelaus and a reluctant general.History actors take a bowSnow Dog!Camo mom working on her next project.

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

There’s the verse that says “love covers a multitude of sins”, well... so does snow:grimacing::flushed::stuck_out_tongue_closed_eyes:. #mudseason #nograssagain #lifewithboysMicah’s been working on his beard. It’s filling in quite nicely!! #mountainman #lifewithboys #11yearoldswithbeardsSick day for Lucas and therefore the rest of us. It’s nice though to have the windows open!!What a difference a haircut makes! Spock to Simon. #beforeandafter #spockhair #haircut #shearingmysheepCelebrating Chinese New Year with a friend (a little early) with some treats straight from Beijing! Yummy!This greeted me this morning. :heart_eyes: #lifewithboys #ivalentinesdayPhotoApparently my read aloud book was riveting to this guy:yum:. The rest of us enjoyed it though. A historical fiction account of Tyndale’s life by Scott O’Dell. #thehawkthatcouldnothuntbyday #readaloud #historicalfictionPhotoThe current favorite face of the house! It’s a cute one!! #puppylifeYour future pilot :man:‍:airplane:Agamemnon, Menelaus and a reluctant general.History actors take a bowSnow Dog!Camo mom working on her next project.