Skip to content

Commit 2049b73

Browse files
committed
Added IP2Proxy web service.
1 parent 7b325e4 commit 2049b73

File tree

3 files changed

+208
-12
lines changed

3 files changed

+208
-12
lines changed

README.md

+51-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ This module allows user to query an IP address if it was being used as open prox
1010
## Methods
1111
Below are the methods supported in this class.
1212

13+
14+
15+
### BIN Database Class
16+
1317
|Method Name|Description|
1418
|---|---|
1519
|open|Open the IP2Proxy BIN data for lookup. Please see the **Usage** section of the 3 modes supported to load the BIN data file.|
@@ -31,15 +35,29 @@ Below are the methods supported in this class.
3135
|getAS|Autonomous system (AS) name.|
3236
|getLastSeen|Proxy last seen in days.|
3337

38+
39+
40+
### Web Service Class
41+
42+
| Method Name | Description |
43+
| ----------- | ------------------------------------------------------------ |
44+
| Constructor | Expect 3 input parameters:<ol><li>IP2Proxy API Key.</li><li>Package (PX1 - PX8)</li><li>Use HTTPS or HTTP</li></ol> |
45+
| lookup | Return the proxy information in array.<ul><li>countryCode</li><li>countryName</li><li>regionName</li><li>cityName</li><li>isp</li><li>domain</li><li>usageType</li><li>asn</li><li>as</li><li>lastSeen</li><li>proxyType</li><li>isProxy</li></ul> |
46+
| getCredit | Return remaining credit of the web service account. |
47+
48+
49+
3450
## Usage
3551

52+
### BIN Database
53+
3654
Open and read IP2Proxy binary database. There are 3 modes:
3755

3856
1. **\IP2Proxy\Database::FILE_IO** - File I/O reading. Slower look, but low resource consuming.
3957
2. **\IP2Proxy\Database::MEMORY_CACHE** - Caches database into memory for faster lookup. Required high memory.
4058
3. **\IP2Proxy\Database::SHARED_MEMORY** - Stores whole IP2Proxy database into system memory. Lookup is possible across all applications within the system. Extremely resources consuming. Do not use this mode if your system do not have enough memory.
4159

42-
```
60+
```php
4361
require 'class.IP2Proxy.php';
4462

4563
$db = new \IP2Proxy\Database();
@@ -95,6 +113,38 @@ echo '<p><strong>Last Seen: </strong>' . $lastSeen . '</p>';
95113

96114
Note: if you are getting error such as `Call to undefined function IP2Proxy\gmp_import()`, you probably did not have the module to install or enable in php.ini. You can check your php.ini to make sure that the module has been enabled.
97115

116+
### Web Service API
117+
118+
To lookup by Web service, you will need to sign up for [IP2Proxy Web Service](https://www.ip2location.com/web-service/ip2proxy) to get a API key.
119+
120+
Start your lookup by following codes:
121+
122+
```php
123+
require 'class.IP2Proxy.php';
124+
125+
// Lookup by Web API
126+
$ws = new \IP2Proxy\WebService('YOUR_API_KEY', 'PX8', false);
127+
128+
$results = $ws->lookup('1.0.241.135');
129+
130+
if ($results !== false) {
131+
echo '<p><strong>Country Code: </strong>' . $results['countryCode'] . '</p>';
132+
echo '<p><strong>Country Name: </strong>' . $results['countryName'] . '</p>';
133+
echo '<p><strong>Region: </strong>' . $results['regionName'] . '</p>';
134+
echo '<p><strong>City: </strong>' . $results['cityName'] . '</p>';
135+
echo '<p><strong>ISP: </strong>' . $results['isp'] . '</p>';
136+
echo '<p><strong>Domain: </strong>' . $results['domain'] . '</p>';
137+
echo '<p><strong>Usage Type: </strong>' . $results['usageType'] . '</p>';
138+
echo '<p><strong>ASN: </strong>' . $results['asn'] . '</p>';
139+
echo '<p><strong>AS: </strong>' . $results['as'] . '</p>';
140+
echo '<p><strong>Last Seen: </strong>' . $results['lastSeen'] . ' Day(s)</p>';
141+
echo '<p><strong>Proxy Type: </strong>' . $results['proxyType'] . '</p>';
142+
echo '<p><strong>Is Proxy: </strong>' . $results['isProxy'] . '</p>';
143+
}
144+
```
145+
146+
147+
98148
# Reference
99149

100150
### Usage Type

class.IP2Proxy.php

+146-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/*
4-
* Copyright (C) 2005-2019 IP2Location.com
4+
* Copyright (C) 2005-2020 IP2Location.com
55
* All Rights Reserved
66
*
77
* This library is free software: you can redistribute it and/or
@@ -31,7 +31,7 @@ class Database
3131
*
3232
* @var string
3333
*/
34-
public const VERSION = '2.1.0';
34+
public const VERSION = '2.2.0';
3535

3636
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3737
// Error field constants ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -481,9 +481,9 @@ class Database
481481
private $year;
482482
private $month;
483483
private $day;
484-
484+
485485
// This variable will be used to hold the raw row of columns's positions
486-
private $raw_positions_row;
486+
private $raw_positions_row;
487487

488488
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
489489
// Default fields //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -858,13 +858,13 @@ protected function lookup($ip, $fields = null, $asNamed = true)
858858
if ($fields === null) {
859859
$fields = $this->defaultFields;
860860
}
861-
861+
862862
// Get the entire row based on the pointer value.
863863
// The length of the row differs based on the IP version.
864-
if (4 === $ipVersion) {
865-
$this->raw_positions_row = $this->read($pointer - 1, $this->columnWidth[4] + 4);
866-
} elseif (6 === $ipVersion) {
867-
$this->raw_positions_row = $this->read($pointer - 1, $this->columnWidth[6]);
864+
if ($ipVersion === 4) {
865+
$this->raw_positions_row = $this->read($pointer - 1, $this->columnWidth[4] + 4);
866+
} elseif ($ipVersion === 6) {
867+
$this->raw_positions_row = $this->read($pointer - 1, $this->columnWidth[6]);
868868
}
869869

870870
// turn fields into an array in case it wasn't already
@@ -1353,7 +1353,7 @@ private function read($pos, $len)
13531353
* @return string
13541354
*/
13551355
private function readString($pos, $additional = 0)
1356-
{
1356+
{
13571357
// Get the actual pointer to the string's head by extract from raw row data.
13581358
$spos = unpack('V', substr($this->raw_positions_row, $pos, 4))[1] + $additional;
13591359

@@ -1749,3 +1749,139 @@ private function binSearch($version, $ipNumber)
17491749
return false;
17501750
}
17511751
}
1752+
1753+
/**
1754+
* IP2Proxy web service class.
1755+
*/
1756+
class WebService
1757+
{
1758+
/**
1759+
* No cURL extension found.
1760+
*
1761+
* @var int
1762+
*/
1763+
public const EXCEPTION_NO_CURL = 10001;
1764+
1765+
/**
1766+
* Invalid API key format.
1767+
*
1768+
* @var int
1769+
*/
1770+
public const EXCEPTION_INVALID_API_KEY = 10002;
1771+
1772+
/**
1773+
* Web service error.
1774+
*
1775+
* @var int
1776+
*/
1777+
public const EXCEPTION_WEB_SERVICE_ERROR = 10003;
1778+
1779+
/**
1780+
* Constructor.
1781+
*
1782+
* @param string $apiKey API key of your IP2Proxy web service
1783+
* @param string $package Supported IP2Proxy package from PX1 to PX8
1784+
* @param bool $useSsl Enable or disabled HTTPS connection. HTTP is faster but less secure.
1785+
*
1786+
* @throws \Exception
1787+
*/
1788+
public function __construct($apiKey, $package = 'PX1', $useSsl = false)
1789+
{
1790+
if (!\extension_loaded('curl')) {
1791+
throw new \Exception(__CLASS__ . ": Please make sure your PHP setup has the 'curl' extension enabled.", self::EXCEPTION_NO_CURL);
1792+
}
1793+
1794+
if (!preg_match('/^[0-9A-Z]{10}$/', $apiKey) && $apiKey != 'demo') {
1795+
throw new \Exception(__CLASS__ . ': Please provide a valid IP2Proxy web service API key.', self::EXCEPTION_INVALID_API_KEY);
1796+
}
1797+
1798+
if (!preg_match('/^PX[0-9]+$/', $package)) {
1799+
$package = 'PX1';
1800+
}
1801+
1802+
$this->apiKey = $apiKey;
1803+
$this->package = $package;
1804+
$this->useSsl = $useSsl;
1805+
}
1806+
1807+
/**
1808+
* This function will look the given IP address up in IP2Proxy web service.
1809+
*
1810+
* @param string $ip IP address to look up
1811+
*
1812+
* @throws \Exception
1813+
*
1814+
* @return array|false
1815+
*/
1816+
public function lookup($ip)
1817+
{
1818+
$response = $this->httpRequest('http://api.ip2proxy.com/?' . http_build_query([
1819+
'key' => $this->apiKey,
1820+
'ip' => $ip,
1821+
'package' => $this->package,
1822+
]));
1823+
1824+
if (($data = json_decode($response, true)) === null) {
1825+
return false;
1826+
}
1827+
1828+
if ($data['response'] != 'OK') {
1829+
throw new \Exception(__CLASS__ . ': ' . $data['response'], self::EXCEPTION_WEB_SERVICE_ERROR);
1830+
}
1831+
1832+
return $data;
1833+
}
1834+
1835+
/**
1836+
* Get the remaing credit in your IP2Proxy web service account.
1837+
*
1838+
* @return int
1839+
*/
1840+
public function getCredit()
1841+
{
1842+
$response = $this->httpRequest('http://api.ip2proxy.com/?' . http_build_query([
1843+
'key' => $this->apiKey,
1844+
'check' => true,
1845+
]));
1846+
1847+
if (($data = json_decode($response, true)) === null) {
1848+
return 0;
1849+
}
1850+
1851+
if (!isset($data['response'])) {
1852+
return 0;
1853+
}
1854+
1855+
return $data['response'];
1856+
}
1857+
1858+
/**
1859+
* Open a remote web address.
1860+
*
1861+
* @param string $url Website URL
1862+
*
1863+
* @return bool|string
1864+
*/
1865+
private function httpRequest($url)
1866+
{
1867+
$ch = curl_init();
1868+
curl_setopt($ch, CURLOPT_URL, $url);
1869+
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
1870+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
1871+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1872+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
1873+
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
1874+
1875+
$response = curl_exec($ch);
1876+
1877+
if (!curl_errno($ch)) {
1878+
curl_close($ch);
1879+
1880+
return $response;
1881+
}
1882+
1883+
curl_close($ch);
1884+
1885+
return false;
1886+
}
1887+
}

example.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
require 'class.IP2Proxy.php';
33

4+
// Lookup by local BIN database
45
$db = new \IP2Proxy\Database();
56
$db->open('./samples/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN.SAMPLE.BIN', \IP2Proxy\Database::FILE_IO);
67

@@ -46,4 +47,13 @@
4647
print_r($records);
4748
echo '</pre>';
4849

49-
$db->close();
50+
$db->close();
51+
52+
// Lookup by Web API
53+
$ws = new \IP2Proxy\WebService('demo', 'PX8', false);
54+
55+
$results = $ws->lookup('1.0.241.135');
56+
57+
echo '<pre>';
58+
print_r($results);
59+
echo '</pre>';

0 commit comments

Comments
 (0)