Use PHP to develop and implement access log recording and analysis of Baidu Wenxin Yiyan API interface

王林
Release: 2023-08-26 20:56:01
Original
1132 people have browsed it

Use PHP to develop and implement access log recording and analysis of Baidu Wenxin Yiyan API interface

Use PHP to develop and implement access log recording and analysis of Baidu Wenxin Yiyan API interface

Yiyan API is a very popular API interface that can be used on web pages Display one sentence, very concise and practical. In daily development, we often need to use this API interface to add some interesting content to the web page. However, for logging and analysis of interfaces, we sometimes need custom implementations.

This article will introduce how to use PHP development to achieve access log recording and analysis of Baidu Wenxin Yiyan API interface. We will use the MySQL database to store the access logs of the interface and analyze them according to different dimensions.

First, we need to create a database table to store the access log of the interface. You can create a table named api_logs, containing the fields id, api, category, created_at. Among them, id is the unique identifier of the log, api is the access path of the interface, category is the type returned by Yiyan API, and created_at is the creation time of the log.

The following is the SQL statement to create the api_logs table:

CREATE TABLE api_logs (
  id INT(11) AUTO_INCREMENT PRIMARY KEY,
  api VARCHAR(255) NOT NULL,
  category VARCHAR(255) NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Copy after login

Next, we need to write PHP code to record the access log of Yiyan API. We can insert logging code after the code segment that accesses Yiyan API. The code example is as follows:

// 获取一言API的返回结果
$result = file_get_contents('https://v1.hitokoto.cn');

// 解析返回结果
$data = json_decode($result, true);

// 获取返回结果中的类型和内容
$category = $data['type'];
$content = $data['hitokoto'];

// 记录日志
$sql = "INSERT INTO api_logs (api, category) VALUES ('https://v1.hitokoto.cn', '$category')";
mysqli_query($conn, $sql);
Copy after login

In the code, we first use the file_get_contents function to obtain the return result of Yiyan API. Then, use the json_decode function to parse the returned result and obtain the type and content. Next, we use MySQLi's function mysqli_query to execute the SQL insert statement and insert the access path and type of the interface into the api_logs table.

Now, we have completed recording the access log of Yiyan API interface. Next, we can analyze the logs. The following is a simple example to count the number of interface accesses of each type:

// 查询每个类型的接口访问次数
$sql = "SELECT category, COUNT(*) AS count FROM api_logs GROUP BY category";
$result = mysqli_query($conn, $sql);

// 输出结果
while ($row = mysqli_fetch_assoc($result)) {
  echo '类型:' . $row['category'] . ',访问次数:' . $row['count'] . '<br>';
}
Copy after login

In the code, we use the GROUP BY clause of SQL to group the category field, and use the COUNT function to count the number of each type Number of interface visits. Then, the query results are traversed through the mysqli_fetch_assoc function, and the type and number of accesses are output.

Through the above code examples, we can achieve access log recording and analysis of Baidu Wenxin Yiyan API interface. Of course, you can also customize and expand it according to specific needs, such as adding more detailed information such as IP address, date, or implementing other statistical analysis functions.

Summary:

This article introduces how to use PHP to develop and implement access log recording and analysis of Baidu Wenxin Yiyan API interface. We store logs through MySQL and write PHP code to record logs and analyze them. I hope this article can help everyone and provide you with some ideas and references for logging during the development process.

The above is the detailed content of Use PHP to develop and implement access log recording and analysis of Baidu Wenxin Yiyan API interface. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!