Last active
February 22, 2018 22:01
-
-
Save ahmadmarafa/689d7f343d5729fc7489699cb1bf7e58 to your computer and use it in GitHub Desktop.
Get client ip and info using public apis ..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Ip | |
{ | |
public function get() | |
{ | |
if (isset($_SERVER['HTTP_CLIENT_IP'])) | |
{ | |
$ip=$_SERVER['HTTP_CLIENT_IP']; | |
} | |
elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) | |
{ | |
$ip=$_SERVER['HTTP_X_FORWARDED_FOR']; | |
} | |
else | |
{ | |
$ip=$_SERVER['REMOTE_ADDR']; | |
} | |
return $ip; | |
} | |
public function info($ip = null , $object = true) | |
{ | |
$return = array() ; | |
$apis = array( | |
"http://ip-api.com/json/{ip}" , | |
"https://freegeoip.net/json/{ip}" , | |
"http://ipinfo.io/{ip}/json" , | |
"http://www.geoplugin.net/json.gp?ip={ip}" , | |
"https://extreme-ip-lookup.com/json/{ip}" , | |
"http://geoip.nekudo.com/api/{ip}" , | |
"https://ipfind.co/?ip={ip}" , | |
"https://ipapi.co/{ip}/json/", | |
"http://apinotes.com/ipaddress/ip.php?ip={ip}", | |
"http://api.hostip.info/country.php?ip={ip}" , | |
) ; | |
$parsers = array( | |
["code" => "countryCode" , "lat" => "lat" , "long" => "lon" , "country" => "country" , "city" => "regionName"], | |
["code" => "country_code" , "lat" => "latitude" , "long" => "longitude" , "country" => "country_name" , "city" => "region_name"] , | |
["code" => "country" , "lat" => "lat" , "long" => "lon" , "country" => "country" , "city" => "city"] , | |
["code" => "geoplugin_countryCode" , "lat" => "geoplugin_latitude" , "long" => "geoplugin_longitude" , "country" => "geoplugin_countryName" , "city" => null] , | |
["code" => "countryCode" , "country" => "country" , "lat" => "lat" , "long"=>"lon" , "city" => "city"], | |
["code" => "country.code" , "country" => "country.name" , "lat" => "location.latitude" , "long"=>"location.longitude" , "city" => null], | |
["code" => "country_code" , "country" => "country_name" , "lat" => "latitude" , "long"=>"longitude" , "city" => "city"], | |
["code" => "country" , "country" => "country_name" , "lat" => "latitude" , "long"=>"longitude" , "city" => "city"] , | |
["code" => "country_code" , "country" => "country_name" , "lat" => "latitude" , "long" => "longitude" , "city" => "city_name"], | |
"string" | |
) ; | |
foreach($apis as $index => $api) | |
{ | |
$url = str_replace("{ip}" , $ip ?: $this->get() , $api) ; | |
if($data = $this->getData($url , $parsers[$index])) | |
{ | |
return $data; | |
} | |
} | |
return false ; | |
} | |
function getData($url , $parse) | |
{ | |
$return = [] ; | |
$contextInputs = array( | |
"http" => array( | |
"method"=>"GET" , | |
"header" => array( | |
"User-Agent: Opera/9.90 (Windows NT 5.1; U; en) Presto/2.6.30 Version/10.60\r\n" . | |
"Accept-Language: en-us\r\n" | |
) | |
) | |
) ; | |
$context = stream_context_create($contextInputs) ; | |
if($data = file_get_contents($url , '' ,$context)) | |
{ | |
if(is_array($parse)) | |
{ | |
$data = json_decode($data , true); | |
if(json_last_error() == 0) | |
{ | |
foreach($parse as $key=>$value) | |
{ | |
$return[$key] = $data[$value] ; | |
} | |
return $return; | |
} | |
return false ; | |
} | |
else | |
{ | |
return ["code" => $data ]; | |
} | |
} | |
return false ; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment