How to use Baidu Wenxin Yiyan API to implement a daily sentence in PHP development

WBOY
Release: 2023-08-27 10:28:02
Original
1293 people have browsed it

How to use Baidu Wenxin Yiyan API to implement a daily sentence in PHP development

How to use Baidu Wenxin Yiyan API to implement a daily sentence in PHP development

A concise and meaningful sentence can give people profound thinking and Inspire. In order to add some inspiration to your website, you can also use Baidu Wenxin Yiyan API to implement the function of one sentence per day. In this way, a different famous saying will be displayed every day, bringing more value and content to the website.

First of all, we need to understand the basic usage of Baidu Wenxin Yiyan API. Baidu Wenxin Yiyan API is a free API interface that provides various types of famous sayings and aphorisms. We can choose different types according to our needs, such as inspirational, friendship, love, etc.

To use Baidu Wenxinyiyan API, we need to apply for an API key first. For the specific application process, please refer to the official documentation of Baidu Wenxin Yiyan API. After the application is completed, we can start writing PHP code to call the API interface.

First, we need to define some basic parameters, including the URL of the API interface and the type to be obtained. Here, we select the type as inspirational. The code is as follows:

$apiUrl = 'https://api.lwl12.com/hitokoto/v1?encode=encode';
$type = 'lizhi';
Copy after login

Next, we need to use the cURL library to send an HTTP request and obtain the data returned by the API. The code is as follows:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl . '&type=' . $type);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
Copy after login

In this code, we initialize a cURL session through the curl_init() function, and then use the curl_setopt() function to set some request options, such as the requested URL and the format of the returned data. Finally, send the HTTP request through the curl_exec() function and get the data returned by the API, and then use the curl_close() function to close the cURL session.

Next, we can parse the data returned by the API and display the famous sayings on the web page. The code is as follows:

$data = json_decode($response, true);
if (!empty($data['data'])) {
    echo $data['data'][0]['hitokoto'];
} else {
    echo '暂无数据';
}
Copy after login

In this code, we use the json_decode() function to parse the JSON format data returned by the API into a PHP array. Then, determine whether there is data in the array, and if so, output a famous aphorism; if not, output a prompt message.

Finally, we can integrate the above code into a PHP function and call this function on the web page to display a daily sentence. The code is as follows:

function getRandomQuote() {
    $apiUrl = 'https://api.lwl12.com/hitokoto/v1?encode=encode';
    $type = 'lizhi';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiUrl . '&type=' . $type);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    $data = json_decode($response, true);
    if (!empty($data['data'])) {
        return $data['data'][0]['hitokoto'];
    } else {
        return '暂无数据';
    }
}

echo getRandomQuote();
Copy after login

In this sample code, we define a function named getRandomQuote(), which is responsible for obtaining famous quotes from the API interface and returning the famous quotes. Then, call this function on the web page to display the daily sentence.

To sum up, it is not complicated to use Baidu Wenxin Yiyan API to realize the function of daily sentence. You only need to apply for an API key and then write PHP code to call the API interface. Through this feature, we can add more content and value to the website and bring a richer experience to users.

The above is the detailed content of How to use Baidu Wenxin Yiyan API to implement a daily sentence in PHP development. For more information, please follow other related articles on the PHP Chinese website!

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!