簡體   English   中英

使用Restler3,如何記錄請求/響應

[英]Using Restler3, how to log requests/responses

當前是否有任何方法可以捕獲傳入的請求和響應,並將其記錄下來? 我看到有“ preCall”和“ postCall”方法,但是,我的某些API方法並非嚴格由HTTP動詞命名。

例如,在我的“玩家”類中,我有一個“ get”方法返回給定的player_id一個玩家,而“ getPlayers”則返回所有玩家。

肯定有!

這是我從其他一些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

}

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM