From 82dc3659b3a0b3ba6d937f9247084facf51800e9 Mon Sep 17 00:00:00 2001 From: "hugh.li" Date: Tue, 21 Nov 2023 22:38:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ehttp.options=5Fby=5Fregexp?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=20(#2761)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 我们的服务器目前没有固定IP(函数计算), 所以获取AccessTokens需要使用有固定IP的代理, 但是我又不想所有的请求都使用这个代理, 所以需要ScopingHttpClient的能力 --- src/Kernel/Traits/InteractWithHttpClient.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Kernel/Traits/InteractWithHttpClient.php b/src/Kernel/Traits/InteractWithHttpClient.php index aa914a9ce..518b77814 100644 --- a/src/Kernel/Traits/InteractWithHttpClient.php +++ b/src/Kernel/Traits/InteractWithHttpClient.php @@ -5,10 +5,11 @@ namespace EasyWeChat\Kernel\Traits; use EasyWeChat\Kernel\HttpClient\RequestUtil; +use EasyWeChat\Kernel\Support\Arr; use Psr\Log\LoggerAwareInterface; use Symfony\Component\HttpClient\HttpClient; +use Symfony\Component\HttpClient\ScopingHttpClient; use Symfony\Contracts\HttpClient\HttpClientInterface; - use function property_exists; trait InteractWithHttpClient @@ -39,7 +40,18 @@ public function setHttpClient(HttpClientInterface $httpClient): static protected function createHttpClient(): HttpClientInterface { - return HttpClient::create(RequestUtil::formatDefaultOptions($this->getHttpClientDefaultOptions())); + $options = $this->getHttpClientDefaultOptions(); + + $optionsByRegexp = Arr::get($options, 'options_by_regexp', []); + unset($options['options_by_regexp']); + + $client = HttpClient::create(RequestUtil::formatDefaultOptions($options)); + + if (! empty($optionsByRegexp)) { + $client = new ScopingHttpClient($client, $optionsByRegexp); + } + + return $client; } /**