Home > Backend Development > PHP Tutorial > Using Baidu Wenxin Yiyan API to implement dynamic sentence display in PHP

Using Baidu Wenxin Yiyan API to implement dynamic sentence display in PHP

WBOY
Release: 2023-08-25 15:26:02
Original
1060 people have browsed it

Using Baidu Wenxin Yiyan API to implement dynamic sentence display in PHP

Using Baidu Wenxin Yiyan API in PHP to realize dynamic sentence display

With the development of the Internet, many websites have higher requirements for user experience. One of the important aspects is the dynamic display of page content. On the website, we can display dynamic content by using third-party APIs, such as Yiyan.com's API. This article will introduce how to use Baidu Wenxin Yiyan API to display dynamic sentences in PHP.

First of all, we need to obtain the access interface of Baidu Wenxin Yiyan API. On the official website of Baidu Wenxin Yiyan, we can find the API document and obtain the interface address. The interface address is generally an HTTP request. We can obtain the returned sentence information by sending an HTTP request.

In PHP, you can send HTTP requests by using the cURL library. The following is a sample code that uses cURL to send a request:

$ch = curl_init();

$url = 'https://api.lwl12.com/hitokoto/v1?encode=json';

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if(curl_error($ch)){
    echo 'Error: ' . curl_error($ch);
}

curl_close($ch);
Copy after login

In the above code, we first use the curl_init() function to initialize a cURL session, and then set the URL address and CURLOPT_RETURNTRANSFER option. Setting CURLOPT_RETURNTRANSFER allows the curl_exec() function to return the requested results instead of outputting them directly. After that, we use the curl_exec() function to execute the cURL request and get the returned results. Finally, we close the cURL session via the curl_close() function.

Next, we can parse the returned JSON data and extract the sentence information. In PHP, you can use the json_decode() function to parse JSON data. The following is a sample code for parsing sentence information:

$data = json_decode($response, true);

if(isset($data['content'])){
    $content = $data['content'];
    echo $content;
}
Copy after login

In the above code, we use the json_decode() function to parse the returned JSON data into an array. Then, we determine whether the sentence information is successfully obtained by determining whether the content key exists in the array. If the content key exists, it is assigned to the $content variable and displayed.

Finally, we can integrate the above two pieces of code to realize the display of dynamic sentences. The following is a complete sample code:

$ch = curl_init();

$url = 'https://api.lwl12.com/hitokoto/v1?encode=json';

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if(curl_error($ch)){
    echo 'Error: ' . curl_error($ch);
}

curl_close($ch);

$data = json_decode($response, true);

if(isset($data['content'])){
    $content = $data['content'];
    echo $content;
}
Copy after login

Through the above code, we can use Baidu Wenxin Yiyan API in PHP to display dynamic sentences. We can embed the code into specific locations on the website and get different sentence information by refreshing the page. This can increase the fun and interactivity of the website and improve user experience.

It should be noted that when using third-party APIs, you must abide by the relevant usage agreements and restrictions. When using Baidu Wenxin Yiyan API, we should follow its official usage regulations and add necessary exception handling mechanisms to the code.

To summarize, this article introduces how to use Baidu Wenxin Yiyan API to display dynamic sentences in PHP. By using the cURL library to send an HTTP request and using the json_decode() function to parse the returned JSON data, we can obtain the sentence information and display it. I hope this article will be helpful to developers who use APIs to implement dynamic content display.

The above is the detailed content of Using Baidu Wenxin Yiyan API to implement dynamic sentence display in PHP. 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