Home > Backend Development > PHP Tutorial > Introduction to the application of Baidu Wenxinyiyan in PHP

Introduction to the application of Baidu Wenxinyiyan in PHP

PHPz
Release: 2023-08-27 15:12:01
Original
1226 people have browsed it

Introduction to the application of Baidu Wenxinyiyan in PHP

Introduction to the application of Baidu Wenxin Yiyan in PHP

Introduction:
Baidu Wenxin Yiyan is a common Chinese random sentence generator. Can be used to add some interesting copywriting to the website. In PHP development, by calling Baidu Wenxinyiyan's API interface, we can easily insert random statements into the website to provide users with a better experience. This article will introduce how to use Baidu Wenxinyiyan in PHP and provide sample code.

1. Obtain the API interface of Baidu Wenxin Yiyan
First, we need to obtain the address of the API interface from the official website of Baidu Wenxin Yiyan. Enter "https://developer.baidu.com/" in the browser, log in to the Baidu Developer Center, and enter the Wen Xin Yi Yan API page. On this page, you can get the address and usage instructions of the API interface.

2. Prepare PHP code
The following is a simple sample code, which is used to randomly obtain a sentence from Baidu Wenxinyiyan's API interface and output it to the web page.

<?php
// Step 1: 请求API接口,获取随机语句
$response = file_get_contents('https://v1.hitokoto.cn?encode=json');

// Step 2: 解析API返回的JSON数据
$data = json_decode($response, true);

// Step 3: 输出随机语句
echo '<div>' . $data['hitokoto'] . '</div>';
?>
Copy after login

The above code sends a GET request through the file_get_contents function to obtain the JSON data returned by the API. Then, use the json_decode function to parse the JSON data and get an array containing random statements. Finally, use the echo statement to output random statements to the web page.

3. Page display effect
The following is a simple HTML page used to display random statements obtained using Baidu Wenxin Yiyan API.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>百度文心一言示例</title>
</head>
<body>
    <div id="quote"></div>

    <script>
    // 使用JavaScript异步加载随机语句
    window.onload = function() {
        fetch('https://v1.hitokoto.cn?encode=json')
        .then(function(response) {
            return response.json();
        })
        .then(function(data) {
            document.getElementById('quote').innerText = data.hitokoto;
        })
        .catch(function(error) {
            console.log(error);
        });
    }
    </script>
</body>
</html>
Copy after login

The above code uses the JavaScript fetch function to send an asynchronous request to obtain the JSON data returned by Baidu Wenxin Yiyan API. When the data is returned, a random statement is inserted into the specified div element in the page.

Conclusion:
By calling Baidu Wenxinyiyan’s API interface, we can easily implement the function of inserting random statements in PHP and JavaScript. This not only adds some interest to the website, but also provides a better experience for users. I hope the introduction and sample code in this article can help everyone.

The above is the detailed content of Introduction to the application of Baidu Wenxinyiyan 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