Wednesday, May 15, 2013

get the geographic location of a IP address

5/15/2013

This post explains you, how to get the geographic location of a IP address from http://freegeoip.net API

geoIPLocation.php

<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

class geoIPLocation{ 
    public $Ip = '0.0.0.0'; 
    public $CountryCode = ''; 
    public $CountryName = ''; 
    public $RegionCode = ''; 
    public $RegionName = ''; 
    public $City = ''; 
    public $ZipCode = ''; 
    public $Latitude = ''; 
    public $Longitude = ''; 
    public $MetroCode = ''; 
    public $AreaCode = ''; 
     
    public function getgeoIPLocation($ip = 0){ 
        if($ip){ 
            $url = 'http://freegeoip.net/xml/'.$ip; 
        } else { 
            $url = 'http://freegeoip.net/xml/'; 
        } 
        $postdata = http_build_query( 
            // just incase we later need to post parameters 
        );         

        $opts = array('http' => 
            array( 
                'method' => 'POST', 
                'header' => 'Content-type: application/x-www-form-urlencoded', 
                'content' => $postdata 
            )         
        ); 

        try{ 
            $context = stream_context_create($opts); 
            $result = file_get_contents($url, false, $context); 
            if(!$result){ 
                throw new Exception('Could not get a response from freegeoip.net'); 
                return false; 
            } else { 
                self::pickDataFromXML($result); 
            } 
        } 
        catch (Exception $e){ 
            echo 'Caught exception: '. $e->getMessage(); 
        }     
        return true; 
    }     
     
    public function pickDataFromXML($file){ 
        $HisVurderingNum = 0;     
        $SalgsPriserNum = 0; 
        $XMLFile = ''; 
        $success = 0; 
        try{         
            $XMLFile = simplexml_load_string($file); 
            if(!$XMLFile){ 
                throw new Exception('Could not load XML file!'); 
            } else { 
                $this->Ip = $XMLFile->Ip; 
                $this->CountryCode = $XMLFile->CountryCode; 
                $this->CountryName = $XMLFile->CountryName; 
                $this->RegionCode = $XMLFile->RegionCode; 
                $this->RegionName = $XMLFile->RegionName; 
                $this->City = $XMLFile->City; 
                $this->ZipCode = $XMLFile->ZipCode; 
                $this->Latitude = $XMLFile->Latitude; 
                $this->Longitude = $XMLFile->Longitude; 
                $this->MetroCode = $XMLFile->MetroCode; 
                $this->AreaCode = $XMLFile->AreaCode; 

                $success = 1; 
                unlink($XMLFile); 
            } 
        } 
        catch (Exception $e){ 
            echo 'Caught exception: '. $e->getMessage(); 
        } 
        return $success; 
    }     
}  


?>





usage of script

example 1 : Using your local IP 


<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

include("geoIPLocation.php");
$geoIPLocation = new geoIPLocation(); 
$geoIPLocation->getgeoIPLocation($_SERVER['REMOTE_ADDR']); 
echo $geoIPLocation->Ip.'
'; 
echo $geoIPLocation->CountryCode.'
'; 
echo $geoIPLocation->CountryName.'
'; 
echo $geoIPLocation->RegionCode.'
'; 
echo $geoIPLocation->RegionName.'
'; 
echo $geoIPLocation->City.'
'; 
echo $geoIPLocation->ZipCode.'
'; 
echo $geoIPLocation->Latitude.'
'; 
echo $geoIPLocation->Longitude.'
'; 
echo $geoIPLocation->MetroCode.'
'; 
echo $geoIPLocation->AreaCode.'
'; 

?>





example 2 : Find the proper IP

<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

include("geoIPLocation.php");
$geoIPLocation->getgeoIPLocation(); 
echo $geoIPLocation->Ip.'
'; 
echo $geoIPLocation->CountryCode.'
'; 
echo $geoIPLocation->CountryName.'
'; 
echo $geoIPLocation->RegionCode.'
'; 
echo $geoIPLocation->RegionName.'
'; 
echo $geoIPLocation->City.'
'; 
echo $geoIPLocation->ZipCode.'
'; 
echo $geoIPLocation->Latitude.'
'; 
echo $geoIPLocation->Longitude.'
'; 
echo $geoIPLocation->MetroCode.'
'; 
echo $geoIPLocation->AreaCode.'
'; 


?>


helpful? Share this

The Editorial Team of 4everTutorials consists of a group of PHP Professionals.

0 comments:

 

© 2014 4everTutorials. All rights resevered.

Back To Top