PHP connects to Baidu Wenxin Yiyan API to obtain multiple filtering methods for specific types of sentences
Introduction: Baidu Wenxin Yiyan is an open sentence API interface. Provides a rich variety of sentence content, including inspirational, chicken soup, programmers, leisure and other types. This article will introduce how to use PHP to connect to Baidu Wenxin Yiyan API, and show a variety of filtering methods to help developers obtain the specific types of sentences they need.
1. Connect to Baidu Wenxin Yiyan API
First, we need to connect to Baidu Wenxin Yiyan API through PHP to obtain the sentence content. The following is a simple PHP code example that implements the connection to the API and the data acquisition function:
<?php // API接口地址 $url = 'https://v1.hitokoto.cn/'; // 创建一个cURL资源 $ch = curl_init(); // 设置URL和其他cURL选项 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 发送HTTP请求获取数据 $response = curl_exec($ch); // 关闭cURL资源 curl_close($ch); // 解析JSON数据 $data = json_decode($response, true); // 输出句子内容 echo $data['hitokoto']; ?>
The above code uses the cURL library to send HTTP requests. After obtaining the sentence content, it is obtained by parsing the JSON data specific sentence content.
2. Obtain specific types of sentences
By default, Baidu Wenxin Yiyan API returns random sentences, including various types of content. If we only want to get a specific type of sentence, we can do it by adding parameters in the API address.
The following is a code example to obtain a specific type of sentence based on type:
<?php $type = 'program'; // 句子类型为程序员 $url = 'https://v1.hitokoto.cn/?c=' . $type; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $response = curl_exec($ch); curl_close($ch); $data = json_decode($response, true); echo $data['hitokoto']; ?>
Just pass the sentence type you want to obtain to the c parameter, such as $type in the above code = 'program'
indicates that the sentence type obtained is programmer.
3. Filter sensitive words
When using Baidu Wenxin Yiyan API to obtain sentences, you may encounter some inappropriate content, such as sensitive words. In order to standardize and purify the sentence content, we can use filtering methods to filter out these sensitive words.
The following is a simple code example for filtering sensitive words:
<?php // 过滤敏感词数组 $filter_words = ['敏感词1', '敏感词2', '敏感词3']; $type = 'program'; // 句子类型为程序员 $url = 'https://v1.hitokoto.cn/?c=' . $type; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $response = curl_exec($ch); curl_close($ch); $data = json_decode($response, true); // 过滤敏感词 $data['hitokoto'] = str_replace($filter_words, '*', $data['hitokoto']); echo $data['hitokoto']; ?>
In the above code, we pass the sensitive words into str_replace
by defining an array of sensitive words. Replace in the function and replace sensitive words with *
to achieve the effect of filtering sensitive words.
Summary:
This article introduces how to use PHP to connect to Baidu Wenxin Yiyan API to obtain specific types of sentences, and shows a variety of filtering methods, including sensitive word filtering. By using these methods, developers can obtain the sentence content they need and perform appropriate filtering on the content. I hope this article will be helpful to PHP developers in connecting to Baidu Wenxin Yiyan API to obtain sentences.
The above is the detailed content of PHP connects Baidu Wenxin Yiyan API to obtain multiple filtering methods for specific types of sentences. For more information, please follow other related articles on the PHP Chinese website!