【问题标题】:Using Restler3, how to log requests/responses使用 Restler3,如何记录请求/响应
【发布时间】:2015-02-06 02:17:31
【问题描述】:

目前是否有任何方法可以捕获传入的请求和响应并记录下来?我看到有 'preCall' 和 'postCall' 方法,但是,我的一些 API 方法不是由 HTTP 动词严格命名的。

例如,在我的“玩家”类中,我有一个“get”方法,它为给定的 player_id 返回一个玩家,还有一个“getPlayers”,它返回所有玩家。

【问题讨论】:

    标签: php restler


    【解决方案1】:

    肯定有!

    这是我从其他一些 SO 问题中拼凑的一些代码:

    $r->onComplete(function () use ($r) {
    // Don't log Luracast Restler Explorer recources calls
    if ( ! preg_match('/resources/', $r->url)) {
        $success = $r->responseCode == 200;        
        $request = $r->getRequestData();
        $info = array(
            'base'              => $r->getBaseUrl(),
            'method'            => $r->requestMethod,
            'url'               => $r->url,
            'api_key'           => NULL,
            'route'             => $r->apiMethodInfo->className.'::'.$r->apiMethodInfo->methodName,
            'data'              => $request['request_data'],
            'ip'                => User::getIpAddress(),
            'referer'           => (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER']: ''),
            'http_status_code'  => $r->responseCode,
            'response'          => $success ? '' : $r->exception->getErrorMessage()
            );
            print_r($info); // replace with your logging function here
    
    }
    

    });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      • 1970-01-01
      • 2014-06-15
      • 2015-10-07
      • 2019-03-16
      • 2014-12-31
      • 2016-05-01
      相关资源
      最近更新 更多