In PHP, we can maintain the health status of HTTP requests through the curl_upkeep() function. In order to ensure that the call records and running status information of this function can be effectively monitored and debugged, logging is a very important link. This article will introduce how to record the call log and running status information of the curl_upkeep() function through PHP.
Logging is not only for debugging code, but also for quickly locate problems when there is a problem in the system. The curl_upkeep() function is usually used to maintain a connection to a remote server, or to send heartbeat requests regularly, etc. We can monitor the execution of requests through logging functions, including the request start and end time, request status, response time, etc.
In PHP, we can use the error_log() function or write log information to a file. We will save logs through file records.
Here is an example of how to add logging in the curl_upkeep() function:
<?php
// Log file path
define('LOG_FILE', '/path/to/logfile.log');
/**
* curl_upkeep function
* Used to send periodic requests to remote servers,Stay connected healthy
*/
function curl_upkeep($url) {
// Get the current time,Record the request start time
$start_time = microtime(true);
log_message("curl_upkeep() function开始implement,TargetURL: $url");
// initializationcURLSession
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// implementcURLask
$response = curl_exec($ch);
// 获取askStatus code
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 获取ask结束时间并计算ask时间
$end_time = microtime(true);
$execution_time = round($end_time - $start_time, 4);
if ($response === false) {
// ask失败,Log error log
log_message("cURL ask失败,error message: " . curl_error($ch), 'ERROR');
} else {
// ask成功,Record the status of the response content
log_message("cURL ask成功,HTTPStatus code: $http_code,Response time: $execution_time Second");
}
// closurecURLSession
curl_close($ch);
}
/**
* 写入日志的function
* @param string $message Log content
* @param string $level Log Level,Default isINFO
*/
function log_message($message, $level = 'INFO') {
// Format log messages
$log_message = "[" . date('Y-m-d H:i:s') . "] [$level] - $message\n";
// Write log messages to file
file_put_contents(LOG_FILE, $log_message, FILE_APPEND);
}
// Sample call
curl_upkeep("https://m66.net/api/healthcheck");
?>
The main function of the curl_upkeep() function is to send HTTP requests to the specified URL to maintain the health of the remote server. In this example, the URL domain is replaced with m66.net .
The log_message() function is used to record log information. We can record different types of information according to the log level (such as INFO, ERROR), which facilitates subsequent queries and analysis.
The log records the start and end times of each request, the HTTP response status code, and the time spent in the request response.
Each time curl_upkeep() is executed, the following information will be recorded in the log file:
[2025-05-07 10:30:15] [INFO] - curl_upkeep() function开始implement,TargetURL: https://m66.net/api/healthcheck
[2025-05-07 10:30:16] [INFO] - cURL ask成功,HTTPStatus code: 200,Response time: 1.2345 Second
If the request fails, the log will record the error message:
[2025-05-07 10:35:15] [ERROR] - cURL ask失败,error message: Could not resolve host
Through the above implementation method, we can effectively record the call log and run status information of the curl_upkeep() function. In development and production environments, logging not only helps debugging, but also quickly locates faults when problems arise.
You can modify the log record content and format according to actual needs, and even output the logs to the database for higher-level monitoring and analysis.