如何在PHP Restler 3中记录所有请求?

编程入门 行业动态 更新时间:2024-10-27 16:28:03
本文介绍了如何在PHP Restler 3中记录所有请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经使用Luracast Restler 3 RC 6( github/Luracast/Restler/tree/3.0.0-RC6 ),现在我想创建一个登录到我的API的日志,以便记录对API的每个请求.我试着几乎到处都在寻找答案,但是没有运气.所以我的问题是:

I have created an API with Luracast Restler 3 RC 6 (github/Luracast/Restler/tree/3.0.0-RC6) and now I want to create a logging into my API so that every request made for the API will be logged. I've tried to search for the answer pretty much everywhere, without luck. So my question is:

如何在Restler 3中创建一个日志记录类/函数,每次发出请求时都会调用该类/函数?应该以某种方式实现到路由中还是什么?我需要类似的东西:

How can I create a logging class/function into Restler 3, which gets called everytime when a request is made? Should it be somehow implemented into routing, or what? I need something like:

logging($route, $http_method, $format, $api_key, $parameters);

此函数/类应获取正在发出的请求的所有可能信息,并将其插入到logging表中,如下所示:

This function/class should get every possible information of the request being made and insert it into logging table, something like this:

+----+---------------------+------------+----------+----------------+--------+---------------------------+ | id | log_time | ip_address | api_key | route | method | parameters | +----+---------------------+------------+----------+----------------+--------+---------------------------+ | 1 | 2015-08-06 14:32:54 | 127.0.0.1 | ASDFasdf | /v1/users/list | GET | all_given_parameters_here | +----+---------------------+------------+----------+----------------+--------+---------------------------+

我并不是要您为我创建用于记录的函数/类,我只需要某种有关如何以及在何处执行此操作的指南,或者甚至可能吗?

I'm not asking you to create me a function/class for logging, I just need some kind of a guidance on how and where should I do this, or is it even possible?

忘了提一下,我还需要记录Method,该方法用于给定的路由:GET,POST,PUT,PATCH或DELETE.也许也是使用的格式(JSON或XML).

Forgot to mention I need to log Method also, that which method is being used for the given route: GET, POST, PUT, PATCH or DELETE. Maybe also the format being used (JSON or XML).

我假设我应该以某种方式扩展Luracast \ Restler \ Routes.php Routes->find(),但是如何以及在何处?

I assume I should extend Luracast\Restler\Routes.php Routes->find() somehow somewhere, but how and where?

推荐答案

您可以为此使用事件处理程序(在调用阶段).参见下面的示例

You can use the event handler (at call stage) for this. See the following example

use Luracast\Restler\Restler; use Luracast\Restler\User; $r = new Restler(); $r->onCall(function () use ($r) { // Don't log Luracast Restler Explorer recources calls if (!preg_match('/resources/', $r->url)) { $info = array( 'base' => $r->getBaseUrl(), 'method' => $r->requestMethod, 'url' => $r->url, 'route' => $r->apiMethodInfo->className.'::'.$r->apiMethodInfo->methodName, 'version' => $r->getRequestedApiVersion(), 'data' => $r->getRequestData(), 'ip' => User::getIpAddress(), ); print_r($info); } }); $r->addAPIClass('Say'); $r->handle();

用记录功能替换print_r

更多推荐

如何在PHP Restler 3中记录所有请求?

本文发布于:2023-11-26 14:20:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634116.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   PHP   Restler

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!