diff --git a/README.md b/README.md index de67db6..b7c46d4 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,10 @@ This module allows user to query an IP address if it was being used as open prox ## Methods Below are the methods supported in this class. + + +### BIN Database Class + |Method Name|Description| |---|---| |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. |getAS|Autonomous system (AS) name.| |getLastSeen|Proxy last seen in days.| + + +### Web Service Class + +| Method Name | Description | +| ----------- | ------------------------------------------------------------ | +| Constructor | Expect 3 input parameters:
  1. IP2Proxy API Key.
  2. Package (PX1 - PX8)
  3. Use HTTPS or HTTP
| +| lookup | Return the proxy information in array. | +| getCredit | Return remaining credit of the web service account. | + + + ## Usage +### BIN Database + Open and read IP2Proxy binary database. There are 3 modes: 1. **\IP2Proxy\Database::FILE_IO** - File I/O reading. Slower look, but low resource consuming. 2. **\IP2Proxy\Database::MEMORY_CACHE** - Caches database into memory for faster lookup. Required high memory. 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. -``` +```php require 'class.IP2Proxy.php'; $db = new \IP2Proxy\Database(); @@ -95,6 +113,38 @@ echo '

Last Seen: ' . $lastSeen . '

'; 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. +### Web Service API + +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. + +Start your lookup by following codes: + +```php +require 'class.IP2Proxy.php'; + +// Lookup by Web API +$ws = new \IP2Proxy\WebService('YOUR_API_KEY', 'PX8', false); + +$results = $ws->lookup('1.0.241.135'); + +if ($results !== false) { + echo '

Country Code: ' . $results['countryCode'] . '

'; + echo '

Country Name: ' . $results['countryName'] . '

'; + echo '

Region: ' . $results['regionName'] . '

'; + echo '

City: ' . $results['cityName'] . '

'; + echo '

ISP: ' . $results['isp'] . '

'; + echo '

Domain: ' . $results['domain'] . '

'; + echo '

Usage Type: ' . $results['usageType'] . '

'; + echo '

ASN: ' . $results['asn'] . '

'; + echo '

AS: ' . $results['as'] . '

'; + echo '

Last Seen: ' . $results['lastSeen'] . ' Day(s)

'; + echo '

Proxy Type: ' . $results['proxyType'] . '

'; + echo '

Is Proxy: ' . $results['isProxy'] . '

'; +} +``` + + + # Reference ### Usage Type diff --git a/class.IP2Proxy.php b/class.IP2Proxy.php index aa8d116..f4991f8 100644 --- a/class.IP2Proxy.php +++ b/class.IP2Proxy.php @@ -1,7 +1,7 @@ defaultFields; } - + // Get the entire row based on the pointer value. // The length of the row differs based on the IP version. - if (4 === $ipVersion) { - $this->raw_positions_row = $this->read($pointer - 1, $this->columnWidth[4] + 4); - } elseif (6 === $ipVersion) { - $this->raw_positions_row = $this->read($pointer - 1, $this->columnWidth[6]); + if ($ipVersion === 4) { + $this->raw_positions_row = $this->read($pointer - 1, $this->columnWidth[4] + 4); + } elseif ($ipVersion === 6) { + $this->raw_positions_row = $this->read($pointer - 1, $this->columnWidth[6]); } // turn fields into an array in case it wasn't already @@ -1353,7 +1353,7 @@ private function read($pos, $len) * @return string */ private function readString($pos, $additional = 0) - { + { // Get the actual pointer to the string's head by extract from raw row data. $spos = unpack('V', substr($this->raw_positions_row, $pos, 4))[1] + $additional; @@ -1749,3 +1749,139 @@ private function binSearch($version, $ipNumber) return false; } } + +/** + * IP2Proxy web service class. + */ +class WebService +{ + /** + * No cURL extension found. + * + * @var int + */ + public const EXCEPTION_NO_CURL = 10001; + + /** + * Invalid API key format. + * + * @var int + */ + public const EXCEPTION_INVALID_API_KEY = 10002; + + /** + * Web service error. + * + * @var int + */ + public const EXCEPTION_WEB_SERVICE_ERROR = 10003; + + /** + * Constructor. + * + * @param string $apiKey API key of your IP2Proxy web service + * @param string $package Supported IP2Proxy package from PX1 to PX8 + * @param bool $useSsl Enable or disabled HTTPS connection. HTTP is faster but less secure. + * + * @throws \Exception + */ + public function __construct($apiKey, $package = 'PX1', $useSsl = false) + { + if (!\extension_loaded('curl')) { + throw new \Exception(__CLASS__ . ": Please make sure your PHP setup has the 'curl' extension enabled.", self::EXCEPTION_NO_CURL); + } + + if (!preg_match('/^[0-9A-Z]{10}$/', $apiKey) && $apiKey != 'demo') { + throw new \Exception(__CLASS__ . ': Please provide a valid IP2Proxy web service API key.', self::EXCEPTION_INVALID_API_KEY); + } + + if (!preg_match('/^PX[0-9]+$/', $package)) { + $package = 'PX1'; + } + + $this->apiKey = $apiKey; + $this->package = $package; + $this->useSsl = $useSsl; + } + + /** + * This function will look the given IP address up in IP2Proxy web service. + * + * @param string $ip IP address to look up + * + * @throws \Exception + * + * @return array|false + */ + public function lookup($ip) + { + $response = $this->httpRequest('http://api.ip2proxy.com/?' . http_build_query([ + 'key' => $this->apiKey, + 'ip' => $ip, + 'package' => $this->package, + ])); + + if (($data = json_decode($response, true)) === null) { + return false; + } + + if ($data['response'] != 'OK') { + throw new \Exception(__CLASS__ . ': ' . $data['response'], self::EXCEPTION_WEB_SERVICE_ERROR); + } + + return $data; + } + + /** + * Get the remaing credit in your IP2Proxy web service account. + * + * @return int + */ + public function getCredit() + { + $response = $this->httpRequest('http://api.ip2proxy.com/?' . http_build_query([ + 'key' => $this->apiKey, + 'check' => true, + ])); + + if (($data = json_decode($response, true)) === null) { + return 0; + } + + if (!isset($data['response'])) { + return 0; + } + + return $data['response']; + } + + /** + * Open a remote web address. + * + * @param string $url Website URL + * + * @return bool|string + */ + private function httpRequest($url) + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_FAILONERROR, 1); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); + + $response = curl_exec($ch); + + if (!curl_errno($ch)) { + curl_close($ch); + + return $response; + } + + curl_close($ch); + + return false; + } +} diff --git a/example.php b/example.php index 3a53135..cbbe92b 100644 --- a/example.php +++ b/example.php @@ -1,6 +1,7 @@ open('./samples/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN.SAMPLE.BIN', \IP2Proxy\Database::FILE_IO); @@ -46,4 +47,13 @@ print_r($records); echo ''; -$db->close(); \ No newline at end of file +$db->close(); + +// Lookup by Web API +$ws = new \IP2Proxy\WebService('demo', 'PX8', false); + +$results = $ws->lookup('1.0.241.135'); + +echo '
';
+print_r($results);
+echo '
';