Lovelock
Lovelock

Reputation: 8105

Storing response from PHP Guzzle as JSON in database (Laravel)

Im calling a vehicle data API at an endpoint and can return the data fine using:

$client = new Client();
$url = "https://uk1.ukvehicledata.co.uk/api/MISC DETAILS;
$result = $client->get($url);

return $result->request;

This is using mock data so the response is:

{
    "Request":{
        "RequestGuid":"",
        "PackageId":""
    "Response":{
        // VEHICLE DATA
    }
}

However, I now wish to store the response in the database but cannot access ->request or ->Request etc.

Using:

return json_encode( (array)$result );

Actually returns the headers from Guzzle and no response data.

Any help?

Upvotes: 0

Views: 555

Answers (1)

Alexey Shokov
Alexey Shokov

Reputation: 5010

You have to convert the body into JSON first:

$jsonArray = json_decode($result->getBody()->getContents(), true);

echo $jsonArray['Request']['RequestGuid'];

Upvotes: 1

Related Questions