Home  >  Article  >  Backend Development  >  PHP implements custom filtering and verification processing of Baidu Wenxinyiyan interface

PHP implements custom filtering and verification processing of Baidu Wenxinyiyan interface

WBOY
WBOYOriginal
2023-08-26 14:27:131513browse

PHP implements custom filtering and verification processing of Baidu Wenxinyiyan interface

PHP implements custom filtering and verification processing of Baidu Wenxin Yiyan interface

When developing web applications, we often need to obtain some random data through the interface Text content is displayed. Baidu Wenxin Yiyan interface is a good choice. It provides a series of beautiful sentences that can be used to decorate web pages or display some prompt information. However, due to the openness and randomness of the interface, we need to filter and verify the returned content to ensure that the displayed content meets our needs and specifications.

Below, we will introduce how to use Baidu Wenxin Yiyan interface in PHP and perform customized filtering and verification on the returned content.

First, we need to obtain the random sentences returned by Baidu Wenxin Yiyan interface. We can use PHP's curl library to achieve this function.

<?php
function getOneWord(){
    $url = 'https://v1.hitokoto.cn';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

$oneWord = getOneWord();
echo $oneWord;
?>

In the above code, we define a getOneWord function, which uses the curl library to send a GET request to obtain the return content of the Baidu Wenxin Yiyan interface, and then returns it. In the main program, we call this function and print the returned result.

Next, we need to filter and verify the returned content. Suppose we require that the length of the returned sentence be between 5 and 20 characters and do not contain sensitive words.

<?php
function filterOneWord($oneWord){
    // 长度校验
    $length = mb_strlen($oneWord);
    if($length < 5 || $length > 20){
        return false;
    }
    
    // 敏感词过滤
    $sensitiveWords = array('敏感词1', '敏感词2', '敏感词3');
    foreach($sensitiveWords as $word){
        if(strpos($oneWord, $word) !== false){
            return false;
        }
    }

    return true;
}

$oneWord = getOneWord();
if(filterOneWord($oneWord)){
    echo $oneWord;
}
else{
    echo '获取失败';
}
?>

In the above code, we define a filterOneWord function, which performs length verification and sensitive word filtering on the incoming sentences. In the length check, we use the mb_strlen function to obtain the length of the string and determine whether it is within the specified range. In sensitive word filtering, we use an array to store sensitive words, and use the strpos function to determine whether the sentence contains sensitive words. If the verification passes, print the sentence, otherwise print a message indicating that the acquisition failed.

Through the above code examples, we can implement custom filtering and verification processing of the content returned by Baidu Wenxin Yiyan interface. Through customized rules, we can ensure that the displayed content meets our needs and specifications and does not contain any sensitive words. In actual applications, we can perform further filtering and verification processing according to specific needs to ensure the security and legality of the content returned by the interface.

In summary, for situations where we need to use an interface to obtain random text content during development, we should perform custom filtering and verification on the content returned by the interface. This ensures that the displayed text content meets requirements and specifications, improving the security and user experience of web applications.

The above is the detailed content of PHP implements custom filtering and verification processing of Baidu Wenxinyiyan interface. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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