Serialized PHP

Deprecated

Use JSON. Almost all PHP sites now support json_decode(), and it's faster than unserialize()

Usage

To receive the response in Serialized PHP serialize() format, send a GET request to

http://ip-api.com/php

You can supply an IP address or domain to lookup, or none to use your current IP address.

Response

The following output is interpreted by PHPs var_export() function

A successful request will return, by default, the following:

array (
  'status' => 'success',
  'country' => 'COUNTRY',
  'countryCode' => 'COUNTRY CODE',
  'region' => 'REGION CODE',
  'regionName' => 'REGION NAME',
  'city' => 'CITY',
  'zip' => ZIP CODE,
  'lat' => LATITUDE,
  'lon' => LONGITUDE,
  'timezone' => 'TIME ZONE',
  'isp' => 'ISP NAME',
  'org' => 'ORGANIZATION NAME',
  'as' => 'AS NUMBER / NAME',
  'query' => 'IP ADDRESS USED FOR QUERY',
)

List of returned values

A failed request will return, by default, the following:

array (
  'status' => 'fail',
  'message' => 'ERROR MESSAGE',
  'query' => 'IP ADDRESS USED FOR QUERY',
)

List of error messages

Selectable return values

Usage limits

Our system will automatically ban any IP addresses doing over 150 requests per minute. To unban your IP click here.

You are free to use ip-api.com for non-commercial use. We do not allow commercial use without prior approval.

For commercial, unlimited use see our pro service.

Examples

http://ip-api.com/php/208.80.152.201

a:14:{s:6:"status";s:7:"success";s:7:"country";s:13:"United States";s:11:"countryCode";s:2:"US";s:6:"region";s:2:"CA";s:10:"regionName";s:10:"California";s:4:"city";s:13:"San Francisco";s:3:"zip";s:5:"94105";s:3:"lat";s:7:"37.7898";s:3:"lon";s:9:"-122.3942";s:8:"timezone";s:19:"America/Los_Angeles";s:3:"isp";s:20:"Wikimedia Foundation";s:3:"org";s:20:"Wikimedia Foundation";s:2:"as";s:28:"AS14907 Wikimedia US network";s:5:"query";s:14:"208.80.152.201";}

PHP Example

<?php
$ip = $_REQUEST['REMOTE_ADDR']; // the IP address to query
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
if($query && $query['status'] == 'success') {
  echo 'Hello visitor from '.$query['country'].', '.$query['city'].'!';
} else {
  echo 'Unable to get location';
}
?>

PHP Class

<?php
 
class IPAPI {
    static $fields = 65535;     // refer to http://ip-api.com/docs/api:returned_values#field_generator
    static $use_xcache = true;  // set this to false unless you have XCache installed (http://xcache.lighttpd.net/)
    static $api = "http://ip-api.com/php/";
 
    public $status, $country, $countryCode, $region, $regionName, $city, $zip, $lat, $lon, $timezone, $isp, $org, $as, $reverse, $query, $message;
 
    public static function query($q) {
        $data = self::communicate($q);
        $result = new static;
        foreach($data as $key => $val) {
            $result->$key = $val;
        }
        return $result;
    }
 
    private function communicate($q) {
        $q_hash = md5('ipapi'.$q);
        if(self::$use_xcache && xcache_isset($q_hash)) {
            return xcache_get($q_hash);
        }
        if(is_callable('curl_init')) {
            $c = curl_init();
            curl_setopt($c, CURLOPT_URL, self::$api.$q.'?fields='.self::$fields);
            curl_setopt($c, CURLOPT_HEADER, false);
            curl_setopt($c, CURLOPT_TIMEOUT, 30);
            curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
            $result_array = unserialize(curl_exec($c));
            curl_close($c);
        } else {
            $result_array = unserialize(file_get_contents(self::$api.$q.'?fields='.self::$fields));
        }
        if(self::$use_xcache) {
            xcache_set($q_hash, $result_array, 86400);
        }
        return $result_array;
    }
}
 
 
// example
$query = IPAPI::query("google.com");
echo 'Country: '.$query->country.', city: '.$query->city;
 
echo '<br><br>debug: <pre>';
var_dump($query);
echo '</pre>';
 
?>