PHP implements Baidu Wenxin Yiyan interface traffic monitoring and malicious request prevention solution

PHPz
Release: 2023-08-26 15:52:01
Original
942 people have browsed it

PHP implements Baidu Wenxin Yiyan interface traffic monitoring and malicious request prevention solution

PHP implements the traffic monitoring and malicious request prevention solution for Baidu Wenxin Yiyan interface

Yiyan interface is a very popular API provided by Baidu. By calling This interface can obtain a random Wen Xin Yi Yan. However, in actual applications, due to the large number of accesses to the interface, it may face problems of excessive traffic or even malicious requests. This article will introduce how to use PHP to implement traffic monitoring and prevent malicious requests for this interface.

First of all, we need to deploy a PHP script on our own server as middleware to receive the client's request and call Baidu Wenxinyiyan's interface. The following is a simple sample code:

<?php
function getOneWord() {
    $url = 'https://api.gushi.ci/all.json';
    $data = file_get_contents($url);
    $result = json_decode($data, true);

    return $result['content'];
}

// 检查IP是否被限制访问(60秒最多访问100次)
function checkIP() {
    $ip = $_SERVER['REMOTE_ADDR'];
    $file = './ip.txt';
    $time = time();

    $lines = file($file);

    // 删除过期的记录
    foreach ($lines as $key => $line) {
        $record = explode(',', $line);

        if ($time - $record[0] > 60) {
            unset($lines[$key]);
        }
    }

    file_put_contents($file, implode($lines));

    // 统计当前IP的请求次数
    $count = 0;
    foreach ($lines as $line) {
        $record = explode(',', $line);

        if ($record[1] == $ip) {
            $count++;
        }
    }

    // 超过限制次数
    if ($count >= 100) {
        return false;
    }

    // 添加新的请求记录
    file_put_contents($file, $time . ',' . $ip . PHP_EOL, FILE_APPEND);

    return true;
}

// 允许跨域访问
header('Access-Control-Allow-Origin: *');

// 检查IP是否被限制访问
if (!checkIP()) {
    die('请求过于频繁,请稍后再试!');
}

// 调用百度文心一言接口
$oneWord = getOneWord();

// 返回结果
echo $oneWord;
?>
Copy after login

In the above code, we first wrote the getOneWord() function, which is used to call the Baidu Wenxin Yiyan interface and return a Wenxin Yiyan Word. Then, the checkIP() function is used to check whether the client's IP address exceeds the access frequency limit. Here we record the IP address in a text file and periodically clear out expired records. If the number of requests for a certain IP address exceeds the limit (up to 100 visits within 60 seconds), an error message will be returned, otherwise a new request record will be added.

Next, we added header('Access-Control-Allow-Origin: *') at the beginning of the PHP script. This is to allow cross-domain access and facilitate client calls. the interface. Finally, we return the corresponding content based on the call result.

To test the effect of this solution, you can save the above code as a PHP file, and then access the file in the browser to get a sentence of text. In order to simulate concurrent requests, you can use multiple browser windows or tools to access this interface at the same time and observe whether you receive an error message indicating that requests are too frequent.

Through the above solution, we have implemented the traffic monitoring and prevention of malicious requests functions of Baidu Wenxin Yiyan interface. Access frequency limits can be adjusted as needed to accommodate actual request volume. At the same time, we have also added support for cross-domain access to this interface to facilitate client calls. The advantages of this method are that it is simple to use, low cost, and can effectively protect the interface from being abused.

I hope the above content will be helpful to you, and I wish your project success!

The above is the detailed content of PHP implements Baidu Wenxin Yiyan interface traffic monitoring and malicious request prevention solution. 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!