PHP connects to Baidu Wenxin Yiyan API to obtain data filtering and filter configuration for specific types of sentences

王林
Release: 2023-08-26 08:44:01
Original
1297 people have browsed it

PHP connects to Baidu Wenxin Yiyan API to obtain data filtering and filter configuration for specific types of sentences

PHP connects to Baidu Wenxin Yiyan API to obtain data filtering and filter configuration for specific types of sentences

[Introduction]
With the development of network technology, API (Application Programming Interface) is used more and more widely. API provides a standardized interface that enables different systems to communicate with each other. Baidu Wenxin Yiyan API is one of the commonly used APIs. It provides the function of obtaining various types of sentences, such as animations, comics, novels, etc. This article will introduce how to use PHP to connect to Baidu Wenxin Yiyan API to obtain data of specific types of sentences, and filter the data and configure filters.

[Access to Baidu Wenxin Yiyan API]
First, we need to register a Baidu developer account and create an application to obtain the API Key and Secret Key. Next, using PHP to connect to Baidu Wenxin Yiyan API requires using the cURL library in the code to send HTTP requests and receive responses.

First, we need to define the request address and parameters of the API, as shown below:

$url = 'https://aip.baidubce.com/rpc/2.0/creation/v1/getSentence';
$params = array(
    'sourceType' => 0, // 句子类型,0为动画,1为漫画,2为小说
    'length' => 10, // 每次返回的句子数量
    'filterFlag' => 1, // 是否进行筛选,1为进行筛选,0为不筛选
    'filterType' => 0, // 筛选类型,0为默认筛选器,1为用户自定义筛选器
    'apiKey' => 'Your API Key',
    'secretKey' => 'Your Secret Key',
);
Copy after login

Next, we use the cURL library to send an HTTP request and process the returned results, as shown below:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

$result = json_decode($response, true);
if ($result && isset($result['error_code']) && $result['error_code'] === 0) {
    // 解析和处理返回的句子数据
    $sentences = $result['results'];
    foreach ($sentences as $sentence) {
        echo $sentence['content'] . PHP_EOL;
    }
} else {
    echo 'API请求失败' . PHP_EOL;
}
Copy after login
Copy after login

In the above code, we first send a POST request to the address of Baidu Wenxin Yiyan API through the cURL library and pass the parameters. Then, use the json_decode function to parse the returned JSON format data into a PHP array. Finally, determine whether the returned result is correct, and traverse and output the content of each sentence.

[Data filtering and filter configuration]
Baidu Wenxin Yiyan API provides data filtering and filter configuration functions, which can filter sentences that do not meet the requirements based on user-defined filtering rules. The following is an example filter configuration:

{
    "filter_type": 1,
    "filter_ranges": [
        {
            "key": "category",
            "type": "equal",
            "value": "电影"
        },
        {
            "key": "source",
            "type": "in",
            "value": ["动画", "漫画"]
        },
        {
            "key": "length",
            "type": "less_than",
            "value": 50
        }
    ]
}
Copy after login

The above configuration will filter out sentences that are classified as movies, whose source is animation or comics, and whose length is less than 50 characters. We can pass this configuration to Baidu Wenxin Yiyan API through parameters, as shown below:

$filter = file_get_contents('filter.json');
$params['filter'] = $filter;
Copy after login

In the above code, we first save the filter configuration in a file named filter.json , and then use the file_get_contents function to read the contents of the configuration file and assign it to the filter parameter in the $params array.

Finally, send the HTTP request to Baidu Wenxin Yiyan API again and process the returned results, as shown below:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

$result = json_decode($response, true);
if ($result && isset($result['error_code']) && $result['error_code'] === 0) {
    // 解析和处理返回的句子数据
    $sentences = $result['results'];
    foreach ($sentences as $sentence) {
        echo $sentence['content'] . PHP_EOL;
    }
} else {
    echo 'API请求失败' . PHP_EOL;
}
Copy after login
Copy after login

In the above code, we only need to add the $filter parameter Just pass the filter configuration. Then, the returned sentence data is parsed and processed again.

[Conclusion]
Through the introduction of this article, we have learned how to use PHP to connect to Baidu Wenxin Yiyan API to obtain data of specific types of sentences, and to filter and configure the filter. This provides a basis and reference for us to develop applications based on Baidu Wenxin Yiyan API. Of course, according to actual needs, we can customize different filter configurations to meet more data filtering needs. Hope this article is helpful to you.

The above is the detailed content of PHP connects to Baidu Wenxin Yiyan API to obtain data filtering and filter configuration for specific types of sentences. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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!