token = $token; $this->secret = $secret; } /** * Initialize connection. */ public function init() { $this->handle = curl_init(); curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->handle, CURLOPT_HTTPHEADER, array( "Content-Type: application/json", "Accept: application/json", "Authorization: Token " . $this->token, "X-Secret: " . $this->secret, )); curl_setopt($this->handle, CURLOPT_POST, 1); } /** * Clean service. * See for details: * - https://dadata.ru/api/clean/address * - https://dadata.ru/api/clean/phone * - https://dadata.ru/api/clean/passport * - https://dadata.ru/api/clean/name * * (!) This is a PAID service. Not included in free or other plans. */ public function clean($type, $value) { $url = $this->clean_url . "/$type"; $fields = array($value); return $this->executeRequest($url, $fields); } /** * Find by ID service. * See for details: * - https://dadata.ru/api/find-party/ * - https://dadata.ru/api/find-bank/ * - https://dadata.ru/api/find-address/ */ public function findById($type, $fields) { $url = $this->suggest_url . "/findById/$type"; return $this->executeRequest($url, $fields); } /** * Reverse geolocation service. * See https://dadata.ru/api/geolocate/ for details. */ public function geolocate($lat, $lon, $count = 10, $radius_meters = 100) { $url = $this->suggest_url . "/geolocate/address"; $fields = array( "lat" => $lat, "lon" => $lon, "count" => $count, "radius_meters" => $radius_meters ); return $this->executeRequest($url, $fields); } /** * Detect city by IP service. * See https://dadata.ru/api/iplocate/ for details. */ public function iplocate($ip) { $url = $this->suggest_url . "/iplocate/address"; $fields = array( "ip" => $ip ); return $this->executeRequest($url, $fields); } /** * Suggest service. * See for details: * - https://dadata.ru/api/suggest/address * - https://dadata.ru/api/suggest/party * - https://dadata.ru/api/suggest/bank * - https://dadata.ru/api/suggest/name * - ... */ public function suggest($type, $fields) { $url = $this->suggest_url . "/suggest/$type"; return $this->executeRequest($url, $fields); } /** * Close connection. */ public function close() { curl_close($this->handle); } private function executeRequest($url, $fields) { curl_setopt($this->handle, CURLOPT_URL, $url); if ($fields != null) { curl_setopt($this->handle, CURLOPT_POST, 1); curl_setopt($this->handle, CURLOPT_POSTFIELDS, json_encode($fields)); } else { curl_setopt($this->handle, CURLOPT_POST, 0); } $result = $this->exec(); $result = json_decode($result, true); return $result; } private function exec() { $result = curl_exec($this->handle); $info = curl_getinfo($this->handle); if ($info['http_code'] == 429) { throw new TooManyRequests(); } elseif ($info['http_code'] != 200) { throw new Exception('Request failed with http code ' . $info['http_code'] . ': ' . $result); } return $result; } } ?>