简体   繁体   English

记录所有API请求和响应+ Slim框架

[英]Log All API request and response + Slim framework

I want to log every request and response for API. 我想记录API的每个请求和响应。

$app->hook('slim.after.router', function () use ($app) { 
    $request = $app->request; 
    $response = $app->response;
    echo "<pre>";print_r($response);die;

});

Here i am getting proper request but when i tried to print out response i am getting 在这里,我得到了适当的要求,但当我试图打印出我得到的回应

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
)

I am able to render proper response in API response but not getting as in logs. 我能够在API响应中呈现正确的响应,但不能在日志中获得。

Any help would be appreciated. 任何帮助,将不胜感激。 Thank you! 谢谢!

use of slim.after instead of slim.after.router have resolved the problem. 使用slim.after而不是slim.after.router解决了这个问题。

As in the documentation 如在文档中

slim.after.router :- This hook is invoked after the router is dispatched, before the Response is sent to the client, and after output buffering is turned off. slim.after.router : - 在调度路由器之后,在响应发送到客户端之前以及关闭输出缓冲之后调用此挂接。 This hook is invoked once during the Slim application lifecycle. 在Slim应用程序生命周期中,此挂钩被调用一次。

slim.after :- This hook is invoked after output buffering is turned off and after the Response is sent to the client. slim.after : - 在关闭输出缓冲之后和将响应发送到客户端之后调用此挂钩。 This hook is invoked once during the Slim application lifecycle. 在Slim应用程序生命周期中,此挂钩被调用一次。

$app->hook('slim.after', function () use ($app) { 
    $request = $app->request; 
    $response = $app->response;
    echo "<pre>";print_r($request);die;
});

which outputs :- 哪个输出: -

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
)

Ofcourse you will get response object too :) 当然你也会得到回应对象:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM