Current Location: Home> Latest Articles> How to Use PHP to Connect to Baidu Wenxin Yiyan API and Fetch Specific Types of Sentences

How to Use PHP to Connect to Baidu Wenxin Yiyan API and Fetch Specific Types of Sentences

gitbox 2025-06-16

1. Introduction

When developing web applications, we often need to fetch external data via APIs. Baidu Wenxin Yiyan API is one such API that provides beautiful and philosophical sentences. This article will detail how to use PHP to connect to the Baidu Wenxin Yiyan API and fetch specific types of sentences.

2. Getting the API Key

First, you need to register an account on the Baidu Cloud Platform, create an application, and obtain the API key. The steps are as follows:

2.1 Registering an Account

Visit the Baidu Cloud Platform's official site:

<span class="fun">https://cloud.baidu.com/</span>

Click the "Register" button in the upper-right corner to complete the registration. Afterward, enter the "Console" page to manage your applications.

2.2 Creating an Application

In the application management page, click the "Create Application" button, fill in the application name, description, and other relevant information, then confirm and create the application.

2.3 Getting the API Key

After the application is successfully created, you can find the API key for your application in the management page. Copy this key for later use.

3. Connecting to the API to Fetch Sentences

To use PHP to connect to Baidu Wenxin Yiyan API, we need to send an HTTP request to retrieve data. The following steps outline how to do this:

3.1 Sending the HTTP Request

We can use the cURL library to send HTTP requests. Here's an example code in PHP:


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://v1.hitokoto.cn/?cat=nature&charset=utf-8&encode=json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

The URL "https://v1.hitokoto.cn/" is the API endpoint. Here, we are fetching a "nature" type sentence, but you can adjust the parameters to fetch other types of sentences as needed.

3.2 Parsing JSON Data

Next, we need to parse the returned JSON data into a PHP array using the `json_decode()` function:


$array = json_decode($response, true);
echo $array['hitokoto'];

The code above simply outputs the fetched sentence. In practice, you can further process this data, such as saving it to a database or displaying it on a webpage.

4. Complete Code Example

Here’s a full example of code that fetches a nature sentence from Baidu Wenxin Yiyan API and saves it into a MySQL database:


<?php
$host = 'localhost'; // Database host address
$user = 'root'; // Database username
$password = 'root'; // Database password
$dbname = 'mydatabase'; // Database name

$conn = mysqli_connect($host, $user, $password, $dbname); // Connect to the database
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

curl_setopt($ch, CURLOPT_URL, "https://v1.hitokoto.cn/?cat=nature&charset=utf-8&encode=json"); // Set HTTP request parameters
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch); // Send the request
curl_close($ch);

$array = json_decode($response, true); // Parse the JSON data
$hitokoto = $array['hitokoto'];

$sql = "INSERT INTO hitokoto (content) VALUES ('$hitokoto')"; // Save the sentence to the database
if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn); // Close the database connection
?>

5. Conclusion

This article demonstrated how to connect to the Baidu Wenxin Yiyan API using PHP to fetch specific types of sentences. The steps included registering for Baidu Cloud Platform, creating an application, obtaining an API key, sending an HTTP request, parsing the returned JSON data, and saving the data into a MySQL database. If you're interested in integrating PHP with APIs, this guide provides a solid foundation for your web development projects.