diff --git a/README.md b/README.md index 1598d82..dd2a9bb 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,15 @@ Create API client instance: > $dadata = new \Dadata\DadataClient($token, $secret); ``` +Create API client instance with proxy: + +```php +> $token = "Replace with Dadata API key"; +> $secret = "Replace with Dadata secret key"; +> $proxy = "http://proxy-host/"; +> $dadata = new \Dadata\DadataClient($token, $secret, $proxy); +``` + Then call API methods as specified below. ## Postal Address diff --git a/src/CleanClient.php b/src/CleanClient.php index 33ac03b..285b489 100644 --- a/src/CleanClient.php +++ b/src/CleanClient.php @@ -6,9 +6,9 @@ class CleanClient extends ClientBase { const BASE_URL = "https://cleaner.dadata.ru/api/v1/"; - public function __construct($token, $secret) + public function __construct($token, $secret, $proxy = '') { - parent::__construct(self::BASE_URL, $token, $secret); + parent::__construct(self::BASE_URL, $token, $secret, $proxy); } public function clean($name, $value) diff --git a/src/ClientBase.php b/src/ClientBase.php index 26ed117..b2f86fe 100644 --- a/src/ClientBase.php +++ b/src/ClientBase.php @@ -6,7 +6,7 @@ abstract class ClientBase { public $client; - public function __construct($baseUrl, $token, $secret = null) + public function __construct($baseUrl, $token, $secret = null, $proxy = '') { $headers = [ "Content-Type" => "application/json", @@ -16,11 +16,15 @@ public function __construct($baseUrl, $token, $secret = null) if ($secret) { $headers["X-Secret"] = $secret; } - $this->client = new \GuzzleHttp\Client([ + $options = [ "base_uri" => $baseUrl, "headers" => $headers, "timeout" => Settings::TIMEOUT_SEC - ]); + ]; + if ($proxy) { + $options['proxy'] = $proxy; + } + $this->client = new \GuzzleHttp\Client($options); } protected function get($url, $query = []) diff --git a/src/DadataClient.php b/src/DadataClient.php index 7b95950..fe5bc54 100644 --- a/src/DadataClient.php +++ b/src/DadataClient.php @@ -8,11 +8,11 @@ class DadataClient private $profile; private $suggestions; - public function __construct($token, $secret) + public function __construct($token, $secret, $proxy = '') { - $this->cleaner = new CleanClient($token, $secret); - $this->profile = new ProfileClient($token, $secret); - $this->suggestions = new SuggestClient($token); + $this->cleaner = new CleanClient($token, $secret, $proxy = ''); + $this->profile = new ProfileClient($token, $secret, $proxy = ''); + $this->suggestions = new SuggestClient($token, $proxy = ''); } public function clean($name, $value) diff --git a/src/ProfileClient.php b/src/ProfileClient.php index c9ffae9..c61ff48 100644 --- a/src/ProfileClient.php +++ b/src/ProfileClient.php @@ -8,9 +8,9 @@ class ProfileClient extends ClientBase { const BASE_URL = "https://dadata.ru/api/v2/"; - public function __construct($token, $secret) + public function __construct($token, $secret, $proxy = '') { - parent::__construct(self::BASE_URL, $token, $secret); + parent::__construct(self::BASE_URL, $token, $secret, $proxy = ''); } public function getBalance() diff --git a/src/SuggestClient.php b/src/SuggestClient.php index 7140315..fecab8c 100644 --- a/src/SuggestClient.php +++ b/src/SuggestClient.php @@ -6,9 +6,9 @@ class SuggestClient extends ClientBase { const BASE_URL = "https://suggestions.dadata.ru/suggestions/api/4_1/rs/"; - public function __construct($token) + public function __construct($token, $proxy = '') { - parent::__construct(self::BASE_URL, $token, null); + parent::__construct(self::BASE_URL, $token, null, $proxy); } public function findAffiliated($query, $count = Settings::SUGGESTION_COUNT, $kwargs = [])