Skip to content

Instantly share code, notes, and snippets.

@marcosraudkett
Last active November 19, 2022 04:58
Show Gist options
  • Save marcosraudkett/f03ca2d4085785d7dcf8b39790bb5f77 to your computer and use it in GitHub Desktop.
Save marcosraudkett/f03ca2d4085785d7dcf8b39790bb5f77 to your computer and use it in GitHub Desktop.
minimal ip-api.com php class

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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment