Why do I get errors in guzzle, despite the fact that everything is fine with the curl?

Advertisements

I have error with Guzzle 7 in PHP 8.2. If i do query by Postman or curl all okey, but i need use guzzle lib or analog php lib.
My code:

$this->client = new Client([
    'base_uri' => 'https://fcm.googleapis.com/fcm/',
    'headers' => [
        'Authorization: key=' . *SERVER_KEY*,
        'Content-Type: application/json'
    ]
]);
$fields = [
    'to' => *USER_TOKEN*,
    'notification' => [
        'title' => 'Title',
        'body' => 'Body',
    ],
    'data' => [
        'icon' => *LOGO*,
        'click_action' => *LINK_TO_SITE*,
    ],
];
try {
    $response = $this->client->post('send', ['json' => $fields]);
    $result = json_decode($response->getBody()->getContents(), null, 512, JSON_THROW_ON_ERROR);
} catch (Exception $e) {
    var_dump($e->getMessage());exit;
}

Error:

PHP Fatal error: Uncaught TypeError: strtolower(): Argument #1 ($string) must be of type string, int given in /var/www/vendor/guzzlehttp/psr7/src/MessageTrait.php:50
Stack trace:
#0 /var/www/vendor/guzzlehttp/psr7/src/MessageTrait.php(50): strtolower(0)
#1 /var/www/vendor/guzzlehttp/guzzle/src/Client.php(454): GuzzleHttp\Psr7\Request->hasHeader(0)
#2 /var/www/vendor/guzzlehttp/guzzle/src/Client.php(326): GuzzleHttp\Client->applyOptions(Object(GuzzleHttp\Psr7\Request), Array)
#3 /var/www/vendor/guzzlehttp/guzzle/src/Client.php(168): GuzzleHttp\Client->transfer(Object(GuzzleHttp\Psr7\Request), Array)
#4 /var/www/vendor/guzzlehttp/guzzle/src/Client.php(187): GuzzleHttp\Client->requestAsync(‘POST’, Object(GuzzleHttp\Psr7\Uri), Array)
#5 /var/www/vendor/guzzlehttp/guzzle/src/ClientTrait.php(95): GuzzleHttp\Client->request(‘POST’, ‘send’, Array)
#6 /var/www/_data/classes/webpush/WebPushService.php(86): GuzzleHttp\Client->post(‘send’, Array)
#7 /var/www/_data/classes/webpush/WebPushService.php(128): Webpush\WebPushService->sendWebpush(Object(Webpush\Pushes\FirstWebPush), Array)
#8 /var/www/cron/webpush.php(34): Webpush\WebPushService->sendWebpushToUsers(Object(Webpush\Pushes\FirstWebPush))
#9 {main}
thrown in /var/www/vendor/guzzlehttp/psr7/src/MessageTrait.php on line 50

>Solution :

The headers array should contain both keys and values, like shown in the documentation. Fix your code like this:

    'headers' => [
        'Authorization' => 'key=' . *SERVER_KEY*,
        'Content-Type' => 'application/json'
    ]

Leave a ReplyCancel reply