\Mobi_Mtld_DA_Carrier_CarrierApiWeb

A small extension to the core CarrierApi to get IP from $_SERVER for current client.

The getProperties() and getProperty() methods in this class extract the most suitable client IP.

Example usage:

$carrierApi = new CarrierApiWeb();
// loading data can be slow - it is recommended wrap the API in a Singleton.
$carrierApi->loadDataFromFile('/path/to/sample.dat');

// get all properties for current client
Properties props = carrierApi.GetProperties(request);

// .... use the properties ....
if (props.ContainsKey("networkOperator") {
   string operatorName = props.Get("networkOperator").AsString();
}

// get a single property
Property countryProp = carrierApi.GetProperty("countryCode");
if (countryProp != null) {
  string countryCode = countryProp.AsString();
}

Summary

Methods
Properties
Constants
__construct()
loadDataFromFile()
getDataFileCopyright()
getDataFileCreationDate()
getDataFileVersion()
getProperties()
getProperty()
getPropertyNames()
getIp()
isPublicIp()
$HEADERS_TO_CHECK
VERSION
MIN_PHP_VERSION
dataLoaded()
propertyNameExists()
extractIp()
No protected properties found
N/A
isIpSpecial()
getIpFromServer()
$ipSpecial
$data
$MISSING_DATA_EX
$INVALID_PROP_EX
N/A

Constants

VERSION

VERSION = 'DEVICEATLAS_API_VERSION'

Carrier API version

MIN_PHP_VERSION

MIN_PHP_VERSION = '5.2.3'

Min PHP version required for this API to work

Properties

$HEADERS_TO_CHECK

$HEADERS_TO_CHECK : 

A list of HTTP headers to choose the original client IP address from. In addition to these the RemoteAddr (REMOTE_ADDR) is also used as a final fallback.

Type

$ipSpecial

$ipSpecial : 

Type

$data

$data : 

Data loaded from the data file

Type

$MISSING_DATA_EX

$MISSING_DATA_EX : 

Error messages

Type

$INVALID_PROP_EX

$INVALID_PROP_EX : 

Error messages

Type

Methods

__construct()

__construct() 

Construct a Carrier API object and load the data file (if path is provided).

Throws

\Exception

If PHP version is less than required

loadDataFromFile()

loadDataFromFile(  $path) 

Load the data file from the provided path. The data file is reloaded every time this method is called.

Parameters

$path

Throws

\IOException

Thrown when there is a problem loading the file.

\Mobi_Mtld_DA_Exception_DataFileException

Thrown when there is a problem with the data file.

getDataFileCopyright()

getDataFileCopyright() : string

Returns the data file copyright text.

Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

Returns

string —

the copyright

getDataFileCreationDate()

getDataFileCreationDate() : string

Returns the data file creation date in ISO8601 format.

Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

Returns

string —

the creationDate

getDataFileVersion()

getDataFileVersion() : string

Returns the version of the data file.

Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

Returns

string —

the version

getProperties()

getProperties(  $param = null) : \Mobi_Mtld_DA_Properties

Get the Carrier properties for the current client.

Parameters

$param
Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

Returns

\Mobi_Mtld_DA_Properties

The found properties or null of no properties found.

getProperty()

getProperty(  $propertyName,   $param = null) : mixed

Get a specific Property from the current client IP found.

Note: if multiple properties are needed for the same IP it is more efficient to call getProperties() once than repeated calls to getProperty().

Parameters

$propertyName
$param

Throws

\Mobi_Mtld_DA_Exception_InvalidPropertyNameException

Thrown if the property name does not exist.

Returns

mixed —

The found property or null if no property found.

getPropertyNames()

getPropertyNames() : array

A set of all the possible property names. The Set contains PropertyName objects that each have a name and an associated data type.

Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

Returns

array —

array of possible property names.

getIp()

getIp(  $keyVals) : string

Get the most suitable IP address from the given keyVals set of HTTP headers. This function checks the headers defined in HEADERS_TO_CHECK.

Parameters

$keyVals
Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

Returns

string —

The most suitable IP or NULL if a suitable IP could not be found.

isPublicIp()

isPublicIp(  $ip) : boolean

An IP address is considered public if it is not in any of the following ranges: <pre> 1) any local address IP: 0

2) a local loopback address range: 127/8

3) a site local address i.e. IP is in any of the ranges: range: 10/8 range: 172.16/12 range: 192.168/16

4) a link local address range: 169.254/16

Parameters

$ip
Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

Returns

boolean —

True if it is a public IP, false if the IP is invalid or is not public.

dataLoaded()

dataLoaded() 

Check if the given propertyName is not null and exists in the data file.

Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

propertyNameExists()

propertyNameExists(  $propertyName) 

Check if the given propertyName is not null and exists in the data file. Calls to this method must be sure that the data object is already loaded.

Parameters

$propertyName

Throws

\Mobi_Mtld_DA_Exception_InvalidPropertyNameException

extractIp()

extractIp(  $headerName,   $headerValue) 

Extracts and cleans an IP address from the headerValue. Some headers such as "X-Forwarded-For" can contain multiple IP addresses such as: clientIP, proxy1, proxy2.

..

This method splits up the headerValue and takes the most appropriate value as the IP.

Parameters

$headerName
$headerValue
Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

isIpSpecial()

isIpSpecial(  $ip) 

Check if ip is a special case return true and put the value in the ipSpecial property

Parameters

$ip
Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

getIpFromServer()

getIpFromServer() : string

Get the most suitable IP address from the given request. This function checks the headers defined in CarrierApi::HEADERS_TO_CHECK.

Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

Returns

string —

The found IP address or null if no IP found.