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

Beautiful summer day in RochesterLicensed to kill...deer that is. And small woodland varmits"Daddy I'm cool" :sunglasses:Mmmm marsh mellows@ladyloki, Look a new one!!! Very english. Almost can't detect the lavender, but very tasty!! I'll get you one for when you visit Rochester if they're still at the store next time I go. #kombucha ##jetlagBackGatwick airport.... how it looks to spend 12 hours there:grimacing:. Kids did an amazing jobs helping cart around ALL our 25+ bags!!! God sent many helpful people along the way. #tryagaintoday #flightcancelled #lifewithboys #25 hours later back in home sweet home. Love is not efficient- both of these guys met me at the airport at 2am!Homeward boundWelcome to the This is our newest addition to the rental property, care of Lucas finding a rock on the floor inside and dutifully trying to throw it out the open door opposite this now shattered pane. Just had the glazier here Friday to get replacement panes for broken Lucas has self captioned this original piece of artwork. He calls it "Broken Glass"! #lifewithboysKids love the rain. Maybe a connection between this and the fact that unless we become like children we will not enter the kingdom of God.Great Britain comes complete with free blackberries!!!! Super sweet. #berrygreatbritain

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

Beautiful summer day in RochesterLicensed to kill...deer that is. And small woodland varmits"Daddy I'm cool" :sunglasses:Mmmm marsh mellows@ladyloki, Look a new one!!! Very english. Almost can't detect the lavender, but very tasty!! I'll get you one for when you visit Rochester if they're still at the store next time I go. #kombucha ##jetlagBackGatwick airport.... how it looks to spend 12 hours there:grimacing:. Kids did an amazing jobs helping cart around ALL our 25+ bags!!! God sent many helpful people along the way. #tryagaintoday #flightcancelled #lifewithboys #25 hours later back in home sweet home. Love is not efficient- both of these guys met me at the airport at 2am!Homeward boundWelcome to the This is our newest addition to the rental property, care of Lucas finding a rock on the floor inside and dutifully trying to throw it out the open door opposite this now shattered pane. Just had the glazier here Friday to get replacement panes for broken Lucas has self captioned this original piece of artwork. He calls it "Broken Glass"! #lifewithboysKids love the rain. Maybe a connection between this and the fact that unless we become like children we will not enter the kingdom of God.Great Britain comes complete with free blackberries!!!! Super sweet. #berrygreatbritain