diff --git a/README.md b/README.md index 9cf0303..d623973 100644 --- a/README.md +++ b/README.md @@ -72,8 +72,16 @@ Route::get('test', 'TestController@lookup'); ### WEB SERVICE -1. To use IP2Proxy Web Service, create a new file called "site_vars.php" in `config` directory. -2. In the site_vars.php, save the following contents: +1. To use IP2Location.io or IP2Proxy Web Service, create a new file called "site_vars.php" in `config` directory. +2. In the site_vars.php, save the following contents for IP2Location.io: +``` + 'your_api_key', // Required. Your IP2Location.io API key. + 'IP2LocationioLanguage' => 'en', // Optional. Refer to https://www.ip2location.io/ip2location-documentation for available languages. +]; +``` +Or save the following contents for IP2Proxy: ```php db = new \IP2Proxy\Database($this->getDatabasePath(), \IP2PROXY\Database::FILE_IO); } else if ($mode == 'ws') { - $apikey = \Config::get('site_vars.IP2ProxyAPIKey'); - $package = (null !== \Config::get('site_vars.IP2ProxyPackage')) ? \Config::get('site_vars.IP2ProxyPackage') : 'PX1'; - $ssl = (null !== \Config::get('site_vars.IP2ProxyUsessl')) ? \Config::get('site_vars.IP2ProxyUsessl') : false; - $this->ws = new \IP2Proxy\WebService($apikey, $package, $ssl); + if (!config()->has('site_vars.IP2LocationioAPIKey')) + { + $apikey = \Config::get('site_vars.IP2ProxyAPIKey'); + $package = (null !== \Config::get('site_vars.IP2ProxyPackage')) ? \Config::get('site_vars.IP2ProxyPackage') : 'PX1'; + $ssl = (null !== \Config::get('site_vars.IP2ProxyUsessl')) ? \Config::get('site_vars.IP2ProxyUsessl') : false; + $this->ws = new \IP2Proxy\WebService($apikey, $package, $ssl); + } } } @@ -31,7 +37,49 @@ public function get($ip, $mode = 'bin') $records = $this->db->lookup($ip, \IP2PROXY\Database::ALL); } else if ($mode == 'ws') { - $records = $this->ws->lookup($ip); + if (config()->has('site_vars.IP2LocationioAPIKey')) + { + // Use IP2Location.io API + $ioapi_baseurl = 'https://api.ip2location.io/?'; + $params = [ + 'key' => \Config::get('site_vars.IP2LocationioAPIKey'), + 'ip' => $ip, + 'lang' => ((config()->has('site_vars.IP2LocationioLanguage')) ? \Config::get('site_vars.IP2LocationioLanguage') : ''), + ]; + // Remove parameters without values + $params = array_filter($params); + $url = $ioapi_baseurl . http_build_query($params); + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_FAILONERROR, 0); + 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)) + { + if (($data = json_decode($response, true)) === null) + { + return false; + } + if (array_key_exists('error', $data)) + { + throw new \Exception(__CLASS__ . ': ' . $data['error']['error_message'], $data['error']['error_code']); + } + return $data; + } + + curl_close($ch); + + return false; + + } else + { + $records = $this->ws->lookup($ip); + } } return $records; }