Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JohnCalimanY's avatar

Laravel api response is not working

I have to consume an old Web API in Laravel.

Response body looks like this:

TRANSACTION_ID: KJASDFYDSF^SDFHJSD/2236
STATUS: OK
DATE: 01/03/18

How can I convert the response into Array using Guzzle 6?

0 likes
6 replies
JohnCalimanY's avatar

@tisuchi I need to get the TRANSACTION_ID, STATUS and other properties from the response. Is it possible to do this automatically with Guzzle or I need to parse them with Regex?

4 likes
tisuchi's avatar

@johncalimany

It's might not the right solution. But I guess it will serve your purpose.

private function parseResponse(\GuzzleHttp\Psr7\Response $response) {
    $body = $response->getBody();
    $body->rewind();
    $content = (string) $body->getContents();
    $lines = explode(PHP_EOL, $content);
    $result = [];

    foreach ($line as $line) {
        $chunks = explode(':', $line);
        $result[trim($chunks[0])] = trim($chunks[1]);
    }

    return $result;
}
5 likes
tisuchi's avatar
tisuchi
Best Answer
Level 70

@johncalimany

Sorry. Actually it's my mistake. I put $line instead of $lines.

Try this-

private function parseResponse(\GuzzleHttp\Psr7\Response $response) {
    $body = $response->getBody();
    $body->rewind();
    $content = (string) $body->getContents();
    $lines = explode(PHP_EOL, $content);
    $result = [];

    foreach ($lines as $line) {
        $chunks = explode(':', $line);
        $result[trim($chunks[0])] = trim($chunks[1]);
    }

    return $result;
}
7 likes

Please or to participate in this conversation.