<?php
/**
* Guards execution of HTTP requests for monitoring purposes.
* This code does not use async logic.
*
* @param callable $callback The function to be executed.
* @return void
*/
function monitorHttpRequest(callable $callback)
{
// Log the request details.
$startTime = microtime(true);
$requestData = func_get_args(); // Get all arguments passed to the function
$requestMethod = $_SERVER['REQUEST_METHOD'];
$requestUrl = $_SERVER['REQUEST_URI'];
$userAgent = $_SERVER['HTTP_USER_AGENT'];
// Execute the callback.
$result = $callback(...$requestData);
$endTime = microtime(true);
$executionTime = $endTime - $startTime;
// Log the execution time and other details.
error_log("Request: Method={$requestMethod}, URL={$requestUrl}, User Agent={$userAgent}, Execution Time={$executionTime} seconds");
//Return the result from the callback
return $result;
}
// Example Usage (replace with your actual function)
/*
function myEndpoint($param1, $param2) {
// Your endpoint logic here.
return "Endpoint processed with: " . $param1 . " and " . $param2;
}
//Call the monitored function
$result = monitorHttpRequest("myEndpoint", "value1", "value2");
echo $result;
*/
?>
Add your comment