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
CodePudding user response:
Solution
Looking at the docs, we see two methods:
getRawBody(https://github.com/line/line-bot-sdk-php/blob/0500634336ec1b524587a978b917b7b3a3679aef/src/LINEBot/Response.php#L74)getJSONDecodedBody(https://github.com/line/line-bot-sdk-php/blob/0500634336ec1b524587a978b917b7b3a3679aef/src/LINEBot/Response.php#L84)
So, one could use either $response->getRawBody() or $response->getJSONDecodedBody(), depending on which of them fits the requirements better.
Unsuccessful approaches
My initial idea was to do it via:
json_decode(json_encode($response), true)['body']
but this does not work, the private members are not generated into the JSON. So, instead of that, my next thought was to use Reflection:
$reflectionClass = new ReflectionClass('LINE\\LINEBot\\Response');
var_dump($reflectionClass->getProperty('body')->getValue($response));
however that did not reach the private property either.
