我想记录API的每个请求和响应。
$app->hook('slim.after.router', function () use ($app) {
$request = $app->request;
$response = $app->response;
echo "<pre>";print_r($response);die;
});
在这里,我收到了正确的请求,但是当我尝试打印答复时,我得到了
Slim\Http\Response Object
(
[status:protected] => 200
[headers] => Slim\Http\Headers Object
(
[data:protected] => Array
(
[Content-Type] => application/json
)
)
[cookies] => Slim\Http\Cookies Object
(
[defaults:protected] => Array
(
[value] =>
[domain] =>
[path] =>
[expires] =>
[secure] =>
[httponly] =>
)
[data:protected] => Array
(
)
)
[body:protected] =>
[length:protected] => 0
)
我能够在API响应中呈现适当的响应,但不能像日志中那样呈现。
任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
使用slim.after
代替slim.after.router
已解决了该问题。
slim.after.router :-在路由器启动后调用此挂钩 在将响应发送到客户端之前和之后调度 输出缓冲已关闭。该挂钩在调用期间被调用一次 苗条的应用程序生命周期。
slim.after :-在关闭输出缓冲后以及将响应发送到客户端之后,将调用此挂钩。这个钩子是 在Slim应用程序生命周期中被调用一次。
$app->hook('slim.after', function () use ($app) {
$request = $app->request;
$response = $app->response;
echo "<pre>";print_r($request);die;
});
输出:-
Slim\Http\Response Object
(
[status:protected] => 200
[headers] => Slim\Http\Headers Object
(
[data:protected] => Array
(
[Content-Type] => application/json
)
)
[cookies] => Slim\Http\Cookies Object
(
[defaults:protected] => Array
(
[value] =>
[domain] =>
[path] =>
[expires] =>
[secure] =>
[httponly] =>
)
[data:protected] => Array
(
)
)
[body:protected] => {"data":{"Authorization":"786876866","user":{"id":"1","user_role":"1"}},"status":200,"success":true,"message":"user authentication successful"}
[length:protected] => 577
)
当然,您也会获得响应对象:)