diff --git a/composer.json b/composer.json index 95241a9c..8ba2cf3d 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "require": { "php": "~8.1.0 || ~8.2.0 || ~8.3.0", "ext-ctype": "*", + "ext-curl": "*", "ext-filter": "*", "ext-json": "*", "ext-mbstring": "*", diff --git a/src/Firebase/Messaging/RequestFactory.php b/src/Firebase/Messaging/RequestFactory.php index a4a83712..7987aebf 100644 --- a/src/Firebase/Messaging/RequestFactory.php +++ b/src/Firebase/Messaging/RequestFactory.php @@ -14,10 +14,13 @@ */ final class RequestFactory { + private bool $environmentSupportsHTTP2; + public function __construct( private readonly RequestFactoryInterface $requestFactory, private readonly StreamFactoryInterface $streamFactory, ) { + $this->environmentSupportsHTTP2 = self::environmentSupportsHTTP2(); } public function createRequest(Message $message, string $projectId, bool $validateOnly): RequestInterface @@ -29,6 +32,10 @@ public function createRequest(Message $message, string $projectId, bool $validat ) ; + if ($this->environmentSupportsHTTP2) { + $request = $request->withProtocolVersion('2.0'); + } + $payload = ['message' => $message]; if ($validateOnly === true) { @@ -38,10 +45,24 @@ public function createRequest(Message $message, string $projectId, bool $validat $body = $this->streamFactory->createStream(Json::encode($payload)); return $request - ->withProtocolVersion('2.0') ->withBody($body) ->withHeader('Content-Type', 'application/json; charset=UTF-8') ->withHeader('Content-Length', (string) $body->getSize()) ; } + + /** + * @see https://github.com/microsoftgraph/msgraph-sdk-php/issues/854 + * @see https://github.com/microsoftgraph/msgraph-sdk-php/pull/1120 + */ + private static function environmentSupportsHTTP2(): bool + { + $features = curl_version()["features"] ?? null; + + return + extension_loaded('curl') + && defined('CURL_VERSION_HTTP2') + && ($features & CURL_VERSION_HTTP2) === CURL_VERSION_HTTP2 + ; + } }