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

Alidnet's avatar

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"}}
0 likes
8 replies
tykus's avatar
$sgCode = data_get(json_decode($response), 'GeneralInformation. SGCode');

or

$data = json_decode($response, true);
$sgCode = $data['GeneralInformation']['SGCode'] ?? null;
Alidnet's avatar

Thanks Tykus

I did the below but it returns null

     $response = json_decode($response->getBody(), true);
       $getlpi = $response["ResponseContent"]['GeneralInformation']['SGCode'] ?? null;
tykus's avatar

Why ResponseContent key?

What do you get here:

$response = json_decode($response->getBody(), true);
dd($response);
Alidnet's avatar

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"
}
tykus's avatar

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;
}
Alidnet's avatar

I get Cannot use object of type stdClass as array from this line

$getlpi = $responseContent['GeneralInformation']['SGCode'] ?? null;
tykus's avatar
tykus
Best Answer
Level 104

Sorry:

$responseContent = json_decode($response["ResponseContent"], true);

Please or to participate in this conversation.