diff --git a/README.md b/README.md index eccad19..d04d81e 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,15 @@ use BushlanovDev\MaxMessengerBot\Api; $api = new Api('YOUR_BOT_API_TOKEN'); +// Использовать свой сертификат +$api = new Api('YOUR_BOT_API_TOKEN', guzzleConfig: ['verify' => '/path/to/cert.crt']); + +// Настроить прокси и таймауты +$api = new Api('YOUR_BOT_API_TOKEN', guzzleConfig: [ + 'timeout' => 60, + 'proxy' => 'tcp://proxy:8080' +]); + // Загрузка файла $fileAttachmentRequest = $api->uploadAttachment( type: UploadType::File, diff --git a/src/Api.php b/src/Api.php index b5be71e..74d81db 100644 --- a/src/Api.php +++ b/src/Api.php @@ -52,6 +52,14 @@ class Api public const string API_VERSION = '1.2.5'; + private const array DEFAULT_GUZZLE_CONFIG = [ + 'timeout' => 10, + 'connect_timeout' => 5, + 'read_timeout' => 10, + 'headers' => ['User-Agent' => 'max-bot-api-client-php/' . self::LIBRARY_VERSION . ' PHP/' . PHP_VERSION], + 'verify' => false + ]; + private const string API_BASE_URL = 'https://platform-api2.max.ru'; private const string METHOD_GET = 'GET'; @@ -92,6 +100,7 @@ class Api * @param ClientApiInterface|null $client Http api client. * @param ModelFactory|null $modelFactory The model factory. * @param LoggerInterface|null $logger PSR LoggerInterface. + * @param array|null $guzzleConfig Additional configuration for the default Guzzle HTTP client. * * @throws InvalidArgumentException */ @@ -100,6 +109,7 @@ public function __construct( ?ClientApiInterface $client = null, ?ModelFactory $modelFactory = null, ?LoggerInterface $logger = null, + ?array $guzzleConfig = null, ) { if (empty($accessToken) && $client === null) { throw new InvalidArgumentException('You must provide either an access token or a client.'); @@ -115,12 +125,10 @@ public function __construct( ); } - $guzzle = new \GuzzleHttp\Client([ - 'timeout' => 10, - 'connect_timeout' => 5, - 'read_timeout' => 10, - 'headers' => ['User-Agent' => 'max-bot-api-client-php/' . self::LIBRARY_VERSION . ' PHP/' . PHP_VERSION], - ]); + $finalGuzzleConfig = $guzzleConfig !== null + ? array_merge(self::DEFAULT_GUZZLE_CONFIG, $guzzleConfig) + : self::DEFAULT_GUZZLE_CONFIG; + $guzzle = new \GuzzleHttp\Client($finalGuzzleConfig); $httpFactory = new \GuzzleHttp\Psr7\HttpFactory(); $client = new Client( $accessToken,