PHP uses Baidu Wenxin Yiyan API interface to display specific types of sentences
In web development, it is often necessary to use some famous quotes, inspirational sentences or random quotations to decorate the page and bring some inspiration and encouragement to users. In this article, we will introduce how to use Baidu Wenxin Yiyan API interface through PHP to display specific types of sentences.
Baidu Wenxin Yiyan is an interface that provides various types of sentences. We can obtain specific types of sentences by calling its API interface. First, we need to register an account on Baidu Developer Platform and apply for an API Key. After obtaining the API Key, we can start writing PHP code to implement the sentence display function.
First, we need to create a PHP file named "quote.php". Next, we need to introduce the PHP SDK of Baidu API into the file. You can download and import the corresponding SDK file on the Baidu Developer Platform. Assume we have saved the SDK file in the "AipSpeech.php" file in the same directory.
Next, we need to set some necessary parameters in the code, including API Key and Secret Key. These parameters can be obtained when creating an application on the Baidu Developer Platform. The following is a complete code example:
<?php require_once 'AipSpeech.php'; // 设置APPID/AK/SK const APP_ID = 'your_app_id'; const API_KEY = 'your_api_key'; const SECRET_KEY = 'your_secret_key'; $client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY); // 调用百度文心一言API接口获取句子 function getQuote($category) { $url = 'https://v1.hitokoto.cn/?c=' . urlencode($category); $quote = file_get_contents($url); $quoteObj = json_decode($quote); return $quoteObj->hitokoto; } // 设置要展示的句子类型 $category = 'inspire'; // 获取句子 $quote = getQuote($category); // 输出句子 echo $quote; ?>
In the code, we first use require_once
to introduce the PHP SDK file of Baidu API. Then, create an instance of AipSpeech by setting the APPID, API Key and Secret Key for calling the API interface. Next, we define a function called "getQuote" to get the sentence. In the function, we obtain the returned JSON data by calling the Baidu Wenxin Yiyan API interface based on a specific type of sentence, and parse it into an object.
In the getQuote
function in the code, we use the file_get_contents
function to obtain the data from the Baidu Wenxin Yiyan API interface by taking the sentence classification as a parameter, and Parse the returned JSON data into an object. Finally, we return the content of the sentence.
In the main code, we set the sentence type to be displayed to "inspire" (inspire class) and get the sentence by calling the getQuote
function. Finally, we directly output the content of the sentence.
After saving the above code as a "quote.php" file, run the file on the web server and you will be able to see a random heuristic sentence.
Summary:
It is not complicated to use Baidu Wenxin Yiyan API interface to display specific types of sentences through PHP. First, we need to apply for the API Key of Baidu Developer Platform and introduce the PHP SDK file of Baidu API. Then, we obtain a specific type of sentence by calling the Baidu Wenxin Yiyan API interface and output it to the page.
In this way, we can easily display specific types of sentences in web development to bring some inspiration and encouragement to users. Whether it is in a personal blog, forum or corporate official website, it can bring a better experience to users.
The above is the detailed content of PHP uses Baidu Wenxin Yiyan API interface to display specific types of sentences. For more information, please follow other related articles on the PHP Chinese website!