Home  >  Article  >  Backend Development  >  PHP connects to Baidu Wenxin Yiyan API to obtain custom sorting and filtering methods for specific types of sentences

PHP connects to Baidu Wenxin Yiyan API to obtain custom sorting and filtering methods for specific types of sentences

PHPz
PHPzOriginal
2023-08-27 12:22:47849browse

PHP connects to Baidu Wenxin Yiyan API to obtain custom sorting and filtering methods for specific types of sentences

PHP connects to Baidu Wenxin Yiyan API to obtain custom sorting and filtering methods for specific types of sentences

When we need to display some sentences on a website or application, Baidu Wenxinyiyan API is a very good choice. It provides various types of sentences, such as inspirational, love, poetry, etc., which can bring different spiritual inspiration and emotional resonance to users. This article will introduce how to use PHP to connect to Baidu Wenxin Yiyan API and implement custom sorting and filtering methods.

First of all, we need to apply for the access key of Baidu Wenxin Yiyan API. Register and create a new application on the Baidu AI Open Platform website, and then obtain the API Key and Secret Key in the application.

Next, we can use PHP's curl library to connect to Baidu Wenxin Yiyan API and obtain sentence data. The following is a simple code example:

<?php
    $url = 'https://aip.baidubce.com/rpc/2.0/creation/v1/get_sentence';
    $api_key = 'YOUR_API_KEY';
    $secret_key = 'YOUR_SECRET_KEY';

    $type = 'love'; // 这里可以根据需要替换成其他类型的句子
    
    $params = [
        'type' => $type
    ];
    
    $headers = [
        'Content-Type: application/json',
        'charset: UTF-8'
    ];
    
    // 生成签名
    $timestamp = time();
    $signature = md5($api_key . $timestamp . $secret_key);
    
    $headers[] = 'X-Mock-Appid: 123456'; // 这里可以根据需要修改成自己的AppID
    $headers[] = 'X-Token: ' . $signature;
    $headers[] = 'X-Timestamp: ' . $timestamp;
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    curl_close($ch);

    // 输出结果
    echo $result;
?>

In this sample code, we will use the love type sentence as an example. You can replace it with other types as needed, such as inspirational, sad, etc. At the same time, you also need to replace YOUR_API_KEY and YOUR_SECRET_KEY with the API Key and Secret Key you obtained on the Baidu AI open platform.

After executing the above code, you will get the returned JSON data, which contains multiple sentences. Now, let's implement custom sorting and filtering methods.

The first is the custom sorting method. Suppose we want to sort by the length of sentences, we can modify the code as follows:

// 输出结果
$result = json_decode($result, true);
$sentences = $result['sentences'];

// 自定义排序方法
function customSort($a, $b) {
    $aLength = mb_strlen($a['content'], 'utf-8');
    $bLength = mb_strlen($b['content'], 'utf-8');
    
    if ($aLength == $bLength) {
        return 0;
    }
    
    return ($aLength < $bLength) ? -1 : 1;
}

// 使用自定义排序方法进行排序
usort($sentences, 'customSort');

// 输出排序后的结果
foreach ($sentences as $sentence) {
    echo $sentence['content'] . "
";
}

In this example, the customSort function is a custom sorting method we defined to compare the lengths of two sentences. The usort function will use this custom sort method for sorting. Finally, we use a foreach loop to iterate through the sorted results and output them.

Next is the custom filtering method. Suppose we want to only display sentences with a length greater than 10, we can modify the code as follows:

// 输出结果
$result = json_decode($result, true);
$sentences = $result['sentences'];

// 自定义过滤方法
function customFilter($sentence) {
    $length = mb_strlen($sentence['content'], 'utf-8');
    
    return $length > 10;
}

// 使用自定义过滤方法进行过滤
$sentences = array_filter($sentences, 'customFilter');

// 输出过滤后的结果
foreach ($sentences as $sentence) {
    echo $sentence['content'] . "
";
}

In this example, the customFilter function is a custom filtering method we defined to determine whether the sentence length is greater than 10. The array_filter function will use this custom filtering method to filter. Finally, we use a foreach loop to iterate through the filtered results and output them.

To sum up, we connect to Baidu Wenxin Yiyan API to obtain specific types of sentences, and implement custom sorting and filtering functions. By modifying the parameters in the code, you can flexibly obtain different types of sentences and sort and filter them according to your needs. I hope this article has provided some help for everyone in using Baidu Wenxinyiyan API in PHP development.

The above is the detailed content of PHP connects to Baidu Wenxin Yiyan API to obtain custom sorting and filtering methods for specific types of sentences. 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