Home >Backend Development >PHP Tutorial >How to implement access log recording and analysis of Baidu Wenxin Yiyan API in PHP development?
How to implement access log recording and analysis of Baidu Wenxin Yiyan API in PHP development?
Baidu Wenxinyiyan API is an interface that provides random Wenxinyiyan, which can be used for website beautification and enlightenment reminders. In the process of using this API, we may record and analyze API access to understand user usage and website performance.
In PHP development, we can implement access log recording and analysis of Baidu Wenxin Yiyan API through the following steps:
The first step is to create a database table that records access logs. Use the following SQL statement to create a table named api_log
in MySQL:
CREATE TABLE api_log ( id INT(11) AUTO_INCREMENT, request_time DATETIME, remote_ip VARCHAR(15), response_code INT(11), PRIMARY KEY (id) );
In this table, we define several fields to store related information: id
is an auto-increment field, request_time
records the request time, remote_ip
records the requested IP address, response_code
records the return code of the API (for future analysis) .
The second step is to write an access logging function. This function will be called every time Baidu Wenxin Yiyan API is called, and the relevant information will be saved in the database. The sample code is as follows:
function logAccess($responseCode) { $requestTime = date('Y-m-d H:i:s'); $remoteIp = $_SERVER['REMOTE_ADDR']; // 连接数据库 $conn = mysqli_connect('localhost', 'username', 'password', 'database_name'); if (!$conn) { die('数据库连接失败:' . mysqli_connect_error()); } // 插入记录 $sql = "INSERT INTO api_log (request_time, remote_ip, response_code) VALUES ('$requestTime', '$remoteIp', '$responseCode')"; if (!mysqli_query($conn, $sql)) { echo "记录访问日志失败:" . mysqli_error($conn); } // 关闭数据库连接 mysqli_close($conn); }
In this code, we first obtain the current request time and remote IP address. Then, connect to the database by calling the mysqli_connect
function, and insert relevant information into the api_log
table through SQL statements. Finally, close the database connection.
The third step is to call the access logging function. Before calling Baidu Wenxin Yiyan API, we need to insert a line of code in the code to call the access logging function. The sample code is as follows:
logAccess(0); // 调用百度文心一言API,省略具体代码 logAccess($response['code']);
In this example, we called the access logging function before and after the Baidu Wenxinyiyan API call, and passed in the corresponding return code.
The fourth step is to analyze the access log. After using Baidu Wenxin Yiyan API for a period of time, we may want to analyze the access logs to understand user usage frequency, popular access IP and other information. The following is a simple access log analysis function example:
function analyzeLogs() { // 连接数据库 $conn = mysqli_connect('localhost', 'username', 'password', 'database_name'); if (!$conn) { die('数据库连接失败:' . mysqli_connect_error()); } // 查询访问频率最高的IP地址 $sql = "SELECT remote_ip, COUNT(*) AS count FROM api_log GROUP BY remote_ip ORDER BY count DESC LIMIT 10"; $result = mysqli_query($conn, $sql); echo "访问频率最高的IP地址: "; while ($row = mysqli_fetch_assoc($result)) { echo $row['remote_ip'] . "(" . $row['count'] . "次) "; } // 关闭数据库连接 mysqli_close($conn); } analyzeLogs();
In this example, we use a SQL query to obtain the IP addresses with the highest access frequency, and print them out in sequence.
The above are the basic steps and sample code to implement access log recording and analysis of Baidu Wenxin Yiyan API. By recording and analyzing access logs, we can better understand user behavior and website performance, and make corresponding optimizations and improvements.
The above is the detailed content of How to implement access log recording and analysis of Baidu Wenxin Yiyan API in PHP development?. For more information, please follow other related articles on the PHP Chinese website!