Home > Article > Backend Development > PHP implements Baidu Wenxin Yiyan interface to prevent repeated requests and data deduplication processing
PHP implements Baidu Wenxin Yiyan interface to prevent repeated requests and data deduplication processing
When developing web applications, we often need to call external interfaces to obtain data. To enrich our applications, the Baidu Wenxin Yiyan interface is a very popular interface that can obtain random sentences of various categories. However, when frequently requesting the interface, we need to consider how to prevent repeated requests and at the same time deduplicate the obtained data to avoid data duplication.
In this article, I will demonstrate how to use PHP to implement Baidu Wenxin Yiyan interface's prevention of repeated requests and data deduplication processing.
First of all, we need to clarify how to use Baidu Wenxin Yiyan interface. We can obtain a sentence by sending a GET request to the following interface URL:
GET http://api.lwl12.com/hitokoto/v1?encode=json
This interface will return the JSON data of a sentence, including the sentence content, source and other information.
Next, we need to consider how to prevent duplicate requests. We can use the cache to store the timestamp of the most recent request and compare it with the current timestamp. If the time interval between the two is short, we can get the result of the previous request directly from the cache without making another request.
function getHitokoto() { $cacheFile = 'cache.txt'; $cacheTime = 60; // 缓存时间,单位为秒 if (file_exists($cacheFile) && time() - filemtime($cacheFile) < $cacheTime) { // 直接从缓存中获取上次请求的结果 $result = file_get_contents($cacheFile); } else { // 发起新的请求 $result = file_get_contents('http://api.lwl12.com/hitokoto/v1?encode=json'); // 将结果保存到缓存文件中 file_put_contents($cacheFile, $result); } return $result; }
In the above code, we first define a cache file name and cache time. In the getHitokoto function, we use the file_exists and filemtime functions to determine whether the cache file exists and whether it has expired. If the cache file exists and has not expired, we get the results directly from the cache; otherwise, we initiate a new request and save the results to the cache file.
Next, we need to consider how to perform data deduplication. We can use the database to store the sentences that have been obtained, and query the database before each request. If the sentence already exists, re-initiate the request until an unrepeated sentence is obtained.
The following is a simple example:
function getHitokoto() { // 连接数据库 $conn = new mysqli('localhost', 'username', 'password', 'database'); // 检查数据库连接是否成功 if ($conn->connect_error) { die('数据库连接失败:' . $conn->connect_error); } // 查询数据库中已有的句子 $existingStatements = $conn->query('SELECT statement FROM hitokoto'); // 将查询结果转换为数组 $existingStatementsArr = []; while ($row = $existingStatements->fetch_assoc()) { $existingStatementsArr[] = $row['statement']; } // 发起请求直到获取到一个未重复的句子 do { $result = file_get_contents('http://api.lwl12.com/hitokoto/v1?encode=json'); $resultArr = json_decode($result, true); $statement = $resultArr['hitokoto']; } while (in_array($statement, $existingStatementsArr)); // 插入新句子到数据库 $conn->query('INSERT INTO hitokoto (statement) VALUES ("' . $statement . '")'); // 关闭数据库连接 $conn->close(); return $result; }
In the above code, we first connect to the database and query the existing sentences. Then, before making the request, we use a do-while loop to determine whether the returned sentence already exists in the database. If it exists, we re-initiate a new request until an unrepeated sentence is obtained. Finally, we insert the new sentence into the database and close the database connection.
Through the above code examples, we can realize the prevention of repeated requests and data deduplication processing of Baidu Wenxin Yiyan interface. Through the use of caching and databases, we can improve request efficiency while avoiding repeatedly retrieving the same sentence. This makes our application more stable and efficient.
To sum up, through reasonable use of cache and database, we can optimize external interface calls and improve the performance and stability of our own applications. During development, we should choose appropriate methods to prevent repeated requests and perform data deduplication based on actual needs.
The above is the detailed content of PHP implements Baidu Wenxin Yiyan interface to prevent repeated requests and data deduplication processing. For more information, please follow other related articles on the PHP Chinese website!