Save specific data from 3rd party api response Hi
I'm trying to get just the SGCode from the 3rd party api response and save it to the db. any ideas on how to get it from the returned result below
{"SearchInformation":{"ReportDate":"20/05/2021 15:01:26","Reference":"Limerock Business Park","SearchDescription":"-26.16970343, 28.16623021","SearchType":"336"},"GeneralInformation":{"SGCode":"T0IR06110000028500000","LatLongCoordinates":"-26.16970343,28.16623021"}}
$sgCode = data_get(json_decode($response), 'GeneralInformation. SGCode');
or
$data = json_decode($response, true);
$sgCode = $data['GeneralInformation']['SGCode'] ?? null;
Thanks Tykus
I did the below but it returns null
$response = json_decode($response->getBody(), true);
$getlpi = $response["ResponseContent"]['GeneralInformation']['SGCode'] ?? null;
Why ResponseContent key?
What do you get here:
$response = json_decode($response->getBody(), true);
dd($response);
Sorry $response->getBody returns the below
{
"ResponseMessage": "Success",
"ResponseContent": "{\"SearchInformation\":{\"ReportDate\":\"20/05/2021 15:30:56\",\"Reference\":\"Limerock Business Park\",\"SearchDescription\":\"-26.16970343, 28.16623021\",\"SearchType\":\"336\"},\"GeneralInformation\":{\"SGCode\":\"T0IR06110000028500000\",\"LatLongCoordinates\":\"-26.16970343,28.16623021\"}}",
"PDFString": "",
"TransactionGUID": "307ab26d-ac35-4c27-ae2b-8dbb93962ad1"
}
ResponseContent value is quoted for some reason, so you need to json_decode that as well:
$response = json_decode($response->getBody(), true);
if (isset($response["ResponseContent"])) {
$responseContent = json_decode($response["ResponseContent"]);
$getlpi = $responseContent['GeneralInformation']['SGCode'] ?? null;
}
I get Cannot use object of type stdClass as array from this line
$getlpi = $responseContent['GeneralInformation']['SGCode'] ?? null;
Sorry:
$responseContent = json_decode($response["ResponseContent"], true);
Please sign in or create an account to participate in this conversation.