How to log all requests in PHP Restler 3?

Asked
Active3 hr before
Viewed126 times

7 Answers

requests
90%

I have created an API with Luracast Restler 3 RC 6 (https://github.com/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:,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:,This function/class should get every possible information of the request being made and insert it into logging table, something like this:, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

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();
88%

I have created an API with Luracast Restler 3 RC 6 (https://github.com/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:,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?,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:,This function/class should get every possible information of the request being made and insert it into logging table, something like this:

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);

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 |
   + -- -- + -- -- -- -- -- -- -- -- -- -- - + -- -- -- -- -- -- + -- -- -- -- -- + -- -- -- -- -- -- -- -- + -- -- -- -- + -- -- -- -- -- -- -- -- -- -- -- -- -- - +
load more v
72%

I have created an API with Luracast Restler 3 RC 6 (https://github.com/Luracast/Restler/tree/3.0.0-RC6) and now I want to create a logging into my API so that e..., How to log all requests in PHP Restler 3? I have created an API with Luracast Restler 3 RC 6 (https://github.com/Luracast/Restler/tree/3.0.0-RC6) and now I want to create a logging into my API so that e... ronaldo76 · saved on 4 months ago ,I have created an API with Luracast Restler 3 RC 6 (https://github.com/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:,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?

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);
load more v
65%

When a user is requesting from the API, I want to log his Apikey, DateTime, Ipaddress, Route, Method, Parameters into a table in my database.,I will go through your code and reply again. Meanwhile see if the answer to http://stackoverflow.com/questions/31853608/how-to-log-all-requests-in-php-restler-3 helps,This is what I'm using to send the error out in this case:,That means it is caused by accessing protected API without proper authentication

Options -MultiViews
DirectoryIndex index.php
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteRule ^$ index.php [QSA,L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
<IfModule mod_php5.c>
 php_flag display_errors Off
</IfModule>
load more v
75%

upgraded from version 3 RC5 for latest PHP support,Supports HTTP request methods HEAD, GET, POST, PUT, DELETE, OPTIONS and PATCH via header or request parameter (method),Support for PHP 7.4 added,Added suggested section for all the supported packages.

You may install Restler by running the create project command in your terminal. Replace {projectName} with your actual project name. It will create a folder with that name and install Restler.

php composer.phar create - project luracast / restler {
   projectName
}
load more v
40%

How to create log function using Restler 3 ,Hello, I have tried the above code. I am new to Restler and I'm having some problem retrieve the value of the api key and parameters from a request. I have to log all the api requests into a database. I am using PHP and MySQL., © githubmemory 2020. All rights reserved. Yes, all of them. That means you, JeffreyBool.

< ? php

function logapi($api, $ip, $route, $method, $parameters) {
   include "../db.php";

   $datetime = date("d-m-Y", "h:i:sa");

   $query = mysqli_query($conn, "INSERT INTO tblapi (Apikey,DatTim,Ipaddress,Route,Method,Parameters) VALUES ('$api','$ip','$datetime','$ip','$route','$method','parameters');");
}

$r = new Restler();

$r - > onCall(function() use($r) {

   if (!preg_match('/resources/', $r - > url)) {
      $info = array(
         'api' => ?? ?? ?? ?? ,
         'ip' => User::getIpAddress(),
         'route' => $r - > apiMethodInfo - > className.
         '::'.$r - > apiMethodInfo - > methodName,
         'method' => $r - > requestMethod,
         'parameters' => $r - > ?? ?? ??

      );
      logapi($info);
   }
});
load more v
22%

Just deal with your business logic in php, restler will take care of the REST!, Restler is a simple and effective multi-format Web API Server written in PHP.Just deal with your business logic in php, restler will take care of the REST! ,Make sure all the requests are routed to index.php by enabling URL Rewriting for your website, Do you think we are missing an alternative of Restler or a related project?

You may install Restler by running the create project command in your terminal. Replace {projectName} with your actual project name. It will create a folder with that name and install Restler.

php composer.phar create - project luracast / restler {
   projectName
}
load more v

Other "requests-undefined" queries related to "How to log all requests in PHP Restler 3?"