Home > Backend Development > PHP Tutorial > How to connect Baidu Wenxin Yiyan API with PHP to obtain random statements and save them to the database

How to connect Baidu Wenxin Yiyan API with PHP to obtain random statements and save them to the database

王林
Release: 2023-08-26 18:06:01
Original
1419 people have browsed it

How to connect Baidu Wenxin Yiyan API with PHP to obtain random statements and save them to the database

How to connect PHP to Baidu Wenxin Yiyan API to obtain random statements and save them to the database

In modern website development, many websites will contain some random display statements functions to make the website more interesting and attractive. The Baidu Wenxin Yiyan API provides a convenient way to obtain random sentences. This article will introduce how to use PHP to connect to Baidu Wenxin Yiyan API and save the obtained random statements into the database.

First, we need to register a Baidu developer account and create a Wen Xin Yi Yan application. After successfully creating the application, we will get an API Key, which will be used for our API calls.

Next, we need to use PHP to make API calls. The following is a code example:

<?php
// API的请求URL
$url = "https://api.lwl12.com/hitokoto/v1.php?type=social";

// 设置请求的header
$header = array(
    "Content-Type: application/json;charset=UTF-8",
    "API-Key: YOUR_API_KEY"
);

// 创建一个CURL对象
$curl = curl_init();

// 设置CURL选项
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// 发送请求并获取响应
$response = curl_exec($curl);

// 关闭CURL连接
curl_close($curl);

// 解析响应的JSON数据
$data = json_decode($response, true);

// 判断请求是否成功
if ($data && isset($data['hitokoto'])) {
    $randomSentence = $data['hitokoto'];
    
    // 可以将$randomSentence保存到数据库中
    // 以下是保存到MySQL数据库的示例代码
    // $connection = mysqli_connect("localhost", "username", "password", "database");
    // $randomSentence = mysqli_real_escape_string($connection, $randomSentence);
    // mysqli_query($connection, "INSERT INTO sentences (sentence) VALUES ('$randomSentence')");
    
    echo $randomSentence;
} else {
    echo "Failed to retrieve random sentence";
}
?>
Copy after login

In the above code, we first define the request URL of the API and set the request header, where YOUR_API_KEY needs to be replaced with what we have in the Baidu Developer Platform Obtained API Key.

Next, we created a CURL object and set the CURL options, including the requested URL, header, and format of the returned data. Then send the request and get the response.

Finally, we parse the response JSON data and obtain random statements from it. We can save this random statement to the database. In the sample code, we demonstrate the method of saving random statements to the MySQL database, which can be modified according to your own needs.

Through the above code example, we can easily use PHP to connect to Baidu Wenxin Yiyan API and save the obtained random statements to the database. In this way, we can display these random sentences in the website to increase the interest and attractiveness of the website.

It should be noted that in actual development, we need to add error handling and security measures to our API requests to ensure the stability and security of the code.

The above is the detailed content of How to connect Baidu Wenxin Yiyan API with PHP to obtain random statements and save them to the database. 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