Home > Backend Development > PHP Tutorial > PHP connects to Baidu Wenxin Yiyan API to obtain the weight control method of specific types of sentences

PHP connects to Baidu Wenxin Yiyan API to obtain the weight control method of specific types of sentences

WBOY
Release: 2023-08-14 21:44:01
Original
756 people have browsed it

PHP connects to Baidu Wenxin Yiyan API to obtain the weight control method of specific types of sentences

PHP connects Baidu Wenxin Yiyan API to obtain the weight control method of specific types of sentences

Yiyan is a very popular sentence acquisition plug-in, which can be used in Used in various applications to display interesting, inspirational or healing sentences. The Baidu Wenxin Yiyan API is a set of officially provided interfaces that allow developers to obtain specific types of Yiyan sentences through the API. This article will introduce how to connect Baidu Wenxin Yiyan API in PHP and control the weight of the obtained sentences according to the needs.

First, we need to register a Baidu developer account and create an application to obtain the API access key. Then, we can start writing PHP code.

//Set API access key and type
$accessKey = 'your_access_key';
$type = 'your_type';

// Construct API request URL
$url = 'https://api.lovelive.tools/api/SentenceSet?setKey=' . $accessKey . '&type=' . $type;

// Send API Request
$response = file_get_contents($url);

// Parse API return result
$result = json_decode($response, true);

//Get a random one Sentence
if ($result['status'] == 200 && isset($result['data'])) {

$sentences = $result['data'];
$sentence = $sentences[array_rand($sentences)];
echo $sentence['text'];
Copy after login

} else {

echo '获取句子失败:' . $result['status'];
Copy after login

}
?>

In the above code example, first we specify the sentence type and API access key to be obtained by setting $accessKey and $type. Then, we construct the URL of the API request and use the file_get_contents function to send an HTTP request to obtain the API return result. Next, we parse the JSON data returned by the API and randomly select a sentence from the returned sentence list for output.

It should be noted that Baidu Wenxin Yiyan API provides multiple types of sentences, and $type can be set according to specific needs. For example, if you want to get inspirational type sentences, you can set $type to 'inspire'. If you want to get healing type sentences, you can set $type to 'healing'. It can be adjusted according to actual needs.

In addition, according to the documentation of Baidu Wenxin Yiyan API, you can also control the return frequency of sentences by setting the weight parameter. You can add the parameter '?weight=' to the API request URL to set the weight. For example, '?weight=2' means returning sentences with twice the weight. In the early stages of the project, it is recommended to keep the weights consistent and then adjust them after the number of sentences gradually increases.

Summary:
This article introduces how to connect Baidu Wenxin Yiyan API in PHP to obtain the weight control method of specific types of sentences. By registering a Baidu developer account, creating an application and obtaining an API access key, we can use the Baidu Wenxin Yiyan API to easily obtain various types of sentences. The code example shows how to construct an API request URL and obtain sentences by parsing the API return results. According to actual needs, you can control the type and weight of the obtained sentences by setting the $type and weight parameters. I hope this article will be helpful to developers using Baidu Wenxin Yiyan API.

The above is the detailed content of PHP connects to Baidu Wenxin Yiyan API to obtain the weight control method of 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