Home  >  Article  >  Backend Development  >  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

WBOY
WBOYOriginal
2023-08-27 10:27:231342browse

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';

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);

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 '暂无数据';
}

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();

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!

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