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
Web Developer
Thanks Nice post i got the proper solution
Sep 17th, 2009
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
Sep 19th, 2011
Reply to “Get Location Information via visitor IP with Yahoo Query Language (YQL) and PHP”