Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Properly Handle json_decode Error with Guzzle Stream in Pusher Library? #390

Open
joenyambura opened this issue Jun 30, 2024 · 0 comments

Comments

@joenyambura
Copy link

I recently encountered an issue with the Pusher PHP Server library in my Laravel application.

The error I faced is:

TypeError: json_decode(): Argument #1 ($json) must be of type string, GuzzleHttp\Psr7\Stream given in /var/www/-my-app/vendor/pusher/pusher-php-server/src/Pusher.php:639

Problem
The error occurs because the json_decode function is receiving a GuzzleHttp\Psr7\Stream instead of a string. This typically happens when working with HTTP responses in Guzzle.

Solution I Found
To resolve this issue, I modified the post method in the Pusher library to ensure the response body is converted to a string before passing it to json_decode. Here’s the modified code:

public function post(string $path, $body, array $params = [])
{
    $path = $this->settings['base_path'] . $path;

    $params['body_md5'] = md5($body);

    $params_with_signature = $this->sign($path, 'POST', $params);

    $headers = [
        'Content-Type' => 'application/json',
        'X-Pusher-Library' => 'pusher-http-php ' . self::$VERSION,
    ];

    try {
        $response = $this->client->post(ltrim($path, '/'), [
            'query' => $params_with_signature,
            'body' => $body,
            'http_errors' => false,
            'headers' => $headers,
            'base_uri' => $this->channels_url_prefix(),
            'timeout' => $this->settings['timeout'],
        ]);
    } catch (ConnectException $e) {
        throw new ApiErrorException($e->getMessage());
    }

    $status = $response->getStatusCode();

    if ($status !== 200) {
        $body = (string) $response->getBody();
        throw new ApiErrorException($body, $status);
    }

    // Convert the response body to a string before decoding
    $responseBodyContents = $response->getBody()->getContents();

    try {
        $response_body = json_decode($responseBodyContents, false, 512, JSON_THROW_ON_ERROR);
    } catch (JsonException $e) {
        throw new PusherException('Data decoding error.');
    }

    return $response_body;
}

My Dilemma
While this solution works, modifying vendor files is not recommended because these changes will be lost during package updates.

How can I fix this permanently to avoid bugs when Pusher package is updated?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant