John Davidson

php - Accessing objects from api response

0 comments
Message:


Hello I get an $response from an API post request, below is the $response


LINE\LINEBot\Response::__set_state(array(
'httpStatus' => 200,
'body' => '{"richMenuId":"richmenu-5a489c22120e48cec70f3c3cd3b318db"}',
'headers' =>
array (
'server' => 'openresty',
'date' => 'Thu, 03 Feb 2022 11:31:51 GMT',
'content-type' => 'application/json',
'content-length' => '58',
'cache-control' => 'no-cache, no-store, max-age=0, must-revalidate',
'expires' => '0',
'pragma' => 'no-cache',
'x-content-type-options' => 'nosniff',
'x-frame-options' => 'DENY',
'x-line-request-id' => '84ababf8-81e9-4cda-aeb0-25d9b44e9b24',
'x-xss-protection' => '1; mode=block',
),
))

Can someone tell me how I could access the "richMenuId" from above object?


I tried to access the object as followed
$response->body->richMenuId


This gives me an error:



Cannot access private property LINE\\LINEBot\\Response::$body



I have also tried changing the object to an array using answers on SO but was not able to solve it.


This is probably a noob question but any input is highly appreciated. Thanks



My Answer:
200,
'headers' =>
array (
'Content-Type' =>
array (
0 => 'application/json',
),
'X-Line-Request-Id' =>
array (
0 => '1234567890',
),
'X-Line-Accepted-Response' =>
array (
0 => 'true',
),
),
'body' => '{"message":"Success"}',
)

To access the 'message' key in the response body, you can use the following code:

php
$response = $response->getJSONDecodedBody();
$message = $response['message'];
echo $message;


This code will decode the JSON response body and extract the value of the 'message' key, which in this case is "Success".

Rate this post

3 of 5 based on 1960 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us