|
| 1 | +# IP2Proxy PHP API |
| 2 | + |
| 3 | +This module allows user to query an IP address if it was being used as open proxy, web proxy, VPN anonymizer and TOR exits. It lookup the proxy IP address using **IP2Proxy API**. |
| 4 | + |
| 5 | + |
| 6 | +## Getting Started |
| 7 | +To begin, an API key is required for this module to function. Find further information at https://www.ip2location.com/ip2proxy-web-service |
| 8 | + |
| 9 | +## Installation |
| 10 | +Add the following to your composer.json file: |
| 11 | + |
| 12 | +``` |
| 13 | +require_once 'class.IP2ProxyAPI.php'; |
| 14 | +``` |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +``` |
| 19 | +<?php |
| 20 | +
|
| 21 | +class IP2ProxyAPI |
| 22 | +{ |
| 23 | + public $response; |
| 24 | + public $ipAddress; |
| 25 | + public $countryCode; |
| 26 | + public $countryName; |
| 27 | + public $regionName; |
| 28 | + public $cityName; |
| 29 | + public $isp; |
| 30 | + public $proxyType; |
| 31 | + public $isProxy; |
| 32 | +
|
| 33 | + protected $apiKey; |
| 34 | + protected $useSSL; |
| 35 | +
|
| 36 | + public function __construct($apiKey = '', $useSSL = false) |
| 37 | + { |
| 38 | + $this->apiKey = $apiKey; |
| 39 | + $this->useSSL = $useSSL; |
| 40 | + } |
| 41 | +
|
| 42 | + public function query($ip) |
| 43 | + { |
| 44 | + if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
| 45 | + return false; |
| 46 | + } |
| 47 | +
|
| 48 | + $response = $this->get('http' . (($this->useSSL) ? 's' : '') . '://api.ip2proxy.com/?' . http_build_query([ |
| 49 | + 'key' => $this->apiKey, |
| 50 | + 'ip' => $ip, |
| 51 | + 'package' => 'PX4', |
| 52 | + 'format' => 'json', |
| 53 | + ])); |
| 54 | +
|
| 55 | + if (($json = json_decode($response)) === null) { |
| 56 | + return false; |
| 57 | + } |
| 58 | +
|
| 59 | + $this->response = (string) $json->response; |
| 60 | + $this->countryCode = (string) $json->countryCode; |
| 61 | + $this->countryName = (string) $json->countryName; |
| 62 | + $this->regionName = (string) $json->regionName; |
| 63 | + $this->cityName = (string) $json->cityName; |
| 64 | + $this->isp = (string) $json->isp; |
| 65 | + $this->proxyType = (string) $json->proxyType; |
| 66 | + $this->isProxy = (string) $json->isProxy; |
| 67 | +
|
| 68 | + return true; |
| 69 | + } |
| 70 | +
|
| 71 | + private function get($url) |
| 72 | + { |
| 73 | + $ch = curl_init(); |
| 74 | +
|
| 75 | + curl_setopt($ch, CURLOPT_URL, $url); |
| 76 | + curl_setopt($ch, CURLOPT_FAILONERROR, 1); |
| 77 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 78 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
| 79 | + curl_setopt($ch, CURLOPT_USERAGENT, 'IP2ProxyAPI_PHP-1.0.0'); |
| 80 | + curl_setopt($ch, CURLOPT_TIMEOUT, 3); |
| 81 | + $response = curl_exec($ch); |
| 82 | +
|
| 83 | + return $response; |
| 84 | + } |
| 85 | +} |
| 86 | +?> |
| 87 | +``` |
| 88 | + |
| 89 | + |
0 commit comments