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

Quote form @amylpollock "Children are always the most creative when they are supposed to be doing something else". #quotes #artforkids #quotesaboutkidsTheir own version of Tower BridgeIt's 8:30am. Kids already in the pool!ProviderYes. It's true. I turned 40 on Wednesday.Sand funToday is our 17th anniversary! Love dancing in the minefields with this woman.Every baby loves bath time!! Big enough for 3 (or 4). #bathtime #lifewithboys #bathtimeinsanityMagazine ad for children's body lotion. Radox??!!??:scream::joy:. #britishbrandingLook what I can do.There's something about this product name that just makes me want to laugh out loud. Guess I can't buy it... since I'm not diabetic:joy: #diabeticvanilla #britishbrandingFor discipline one of my kids was to do some copying. I asked him to bring me a piece of paper and pencil with a clipboard. This was what I got... funny guy!! #lifewithboysBoys playing with Simon's bday present.Captivated audience as Simon opens his presents. #9thbirthdayMaking a mast together. #teamwork #chathamhistoricdockyards #brothers

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

Quote form @amylpollock "Children are always the most creative when they are supposed to be doing something else". #quotes #artforkids #quotesaboutkidsTheir own version of Tower BridgeIt's 8:30am. Kids already in the pool!ProviderYes. It's true. I turned 40 on Wednesday.Sand funToday is our 17th anniversary! Love dancing in the minefields with this woman.Every baby loves bath time!! Big enough for 3 (or 4). #bathtime #lifewithboys #bathtimeinsanityMagazine ad for children's body lotion. Radox??!!??:scream::joy:. #britishbrandingLook what I can do.There's something about this product name that just makes me want to laugh out loud. Guess I can't buy it... since I'm not diabetic:joy: #diabeticvanilla #britishbrandingFor discipline one of my kids was to do some copying. I asked him to bring me a piece of paper and pencil with a clipboard. This was what I got... funny guy!! #lifewithboysBoys playing with Simon's bday present.Captivated audience as Simon opens his presents. #9thbirthdayMaking a mast together. #teamwork #chathamhistoricdockyards #brothers