Skip to content

Malformed UTF-8 characters, possibly incorrectly encoded #151

@guldil

Description

@guldil

Hello,

First time after 18 months i got this error.

I'm exporting billing details with $detail = $conn->get('/me/bill/' . $id . '/details/' . $detailid);

Today on my VOIP invoice i got error : "Malformed UTF-8 characters, possibly incorrectly encoded"

After some debug with ChatGPT it's come from this kind of detail :

string(278) "{"periodEnd":"2024-08-01","totalPrice":{"currencyCode":"EUR","value":0,"text":"0.00 €"},"quantity":"1","periodStart":"2024-07-01","domain":"XXXXXXXXXXXXX","billDetailId":"XXXXXXXXXXXX","unitPrice":{"text":"0 €","currencyCode":"EUR","value":0},"description":"Un num▒ro port▒"}"

chatGPT rewrote the function :

private function decodeResponse(Response $response)
{
    if ($response->getStatusCode() === 204 || $response->getBody()->getSize() === 0) {
        return null;
    }
    $response->getBody()->rewind();
    try {
        $bodyContent = $response->getBody()->getContents();
        if (!mb_check_encoding($bodyContent, 'UTF-8')) {
            $bodyContent = mb_convert_encoding($bodyContent, 'UTF-8', 'ISO-8859-1'); // Exemple de conversion
            if (!mb_check_encoding($bodyContent, 'UTF-8')) {
                throw new \Exception('Failed to convert response body to valid UTF-8');
            }
        }

        return json_decode($bodyContent, true, 512, JSON_THROW_ON_ERROR);
    } catch (\Exception $e) {
        var_dump($response->getBody()->getSize());
        var_dump($response->getStatusCode());

        // Réinitialiser le curseur du flux avant de le lire à nouveau pour le débogage
        $response->getBody()->rewind();
        var_dump($response->getBody()->getContents());

        die($e->getMessage());
    }
}`

Thanks

Guldil

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions