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

daf_phillips's avatar

Handling Guzzle GET response data

Hi all,

After reading countless threads and the guzzle documentation itself I've hit a wall.

My current code:- ApiController

<?php

namespace App\Http\Controllers;

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Illuminate\Http\Request;

class ApiController extends Controller
{
    public function getRequest()
    {
        $client = new \GuzzleHttp\Client();
        $request = $client->get('http://www.mocky.io/v2/5cfcef873200004a00ccd468');
        $response = $request->getBody()->getContents();
        dd($response);
    }
}

Which returns a JSON, I believe as a string as follows:-

"""
{ \n
    "make": "Ford",\n
    "model": "Focus",\n
    "VRM": "AB12CDE",\n
    "VIN": "XXXXXXXXXXXXXXXX",\n
}
"""

Now I'm struggling to handle this response, what I need is to get the parameter data, make, model, VRM and VIN then match it to data stored within my database. Struggling at present to get data from this JSON string.

Any help would be appreciated.

Thanks

0 likes
1 reply
mushood's avatar

I'm not sure that you are supposed to have the "\n" in your response.

I used this json validator (https://jsonlint.com/) and found that a valid JSON would be

{
    "make": "Ford",
    "model": "Focus",
    "VRM": "AB12CDE",
    "VIN": "XXXXXXXXXXXXXXXX"
}

So this should work:

$str=str_replace("\r\n","",$response);
$array_response = json_decode($str, true);
$make = $array_response['make'];

Now you have an array and you can easily access the make like explained

Please or to participate in this conversation.