Usage:
<?php
require_once "IpApi.class.php";
//$ip_address = '' if you wish to play around (add your choosing ip address here and uncomment this line and comment the one above)
$result = (new IpApi())->get_info();
print_r($result);
?>
<?php | |
/** | |
* (ip-api.com) | |
*/ | |
class IpApi | |
{ | |
/** | |
* @var string | |
*/ | |
protected $ip_address; | |
public $endpoint = "http://ip-api.com/"; | |
public $parameters = "?fields=status,message,country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,reverse,mobile,proxy,query,district,fsf"; | |
/** | |
* Class Constructor | |
* @param string $ip_address | |
*/ | |
public function __construct() | |
{ | |
$this->ip_address = (isset($this->ip_address)) ? $_SERVER['REMOTE_ADDR'] : $this->ip_address; | |
} | |
public function get_info($ip_address = "") | |
{ | |
if (isset($ip_address) && $ip_address != "") $this->ip_address = $ip_address; | |
$query = file_get_contents($this->endpoint . 'json/' . $this->ip_address . $this->parameters); | |
$json_decode = json_decode($query, true); | |
if (isset($json_decode['status']) && $json_decode['status'] == 'success') { | |
$result = []; | |
foreach($json_decode as $k => $i) { | |
$result[$k] = $i; | |
} | |
} else { | |
$result = ["status" => "ERROR"]; | |
} | |
return $result; | |
} | |
} |