简体   繁体   中英

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?

You have to convert the body into JSON first:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM