Current Location: Home> Latest Articles> How to Use PHP to Connect to Baidu Wenxin Yiyan API to Retrieve Specific Type Sentences and Save to Database

How to Use PHP to Connect to Baidu Wenxin Yiyan API to Retrieve Specific Type Sentences and Save to Database

gitbox 2025-06-16

1. Introduction

In web application development, we often need to fetch data through APIs. The Baidu Wenxin Yiyan API provides a wide range of beautiful and philosophical sentences. This article will guide you through connecting to the Baidu Wenxin Yiyan API using PHP and retrieving specific types of sentences.

2. Obtaining the API Key

To use the Baidu Wenxin Yiyan API, you first need to register on the Baidu Cloud platform and create an application to obtain the API key. Here are the steps:

2.1 Register an Account

Go to the Baidu Cloud official website and click the "Register" button in the top-right corner. After registering, go to the "Console" page.

2.2 Create an Application

In the console, click the "Create Application" button, fill in the application name and description, and confirm to create the application.

2.3 Obtain the API Key

After the application is created, you will be able to find the API key for the application in the console. Copy the key for future use.

3. Connect to the API and Retrieve Sentences

Using PHP, you can send HTTP requests to retrieve sentences from the Baidu Wenxin Yiyan API. Below are the steps:

3.1 Sending an HTTP Request

We can use the cURL library in PHP to send HTTP requests and retrieve the response from the API. Below is a code example:


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

In the code above, `https://v1.hitokoto.cn/` is the API URL, and we are choosing to retrieve sentences from the "cat=nature" category. You can modify the parameters to retrieve other types of sentences.

3.2 Parsing the JSON Data

The response from the API is in JSON format. We can use the `json_decode()` function in PHP to parse it into an associative array for further manipulation.


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

The code above simply outputs the retrieved sentence. In a real-world application, you might want to save the data to a database or display it on a webpage.

4. Complete Code Example

Below is a complete example demonstrating how to retrieve a sentence from the Baidu Wenxin Yiyan API and save it to a MySQL database:


<?php
$host = 'localhost';  // Database host
$user = 'root';       // Database username
$password = 'root';   // Database password
$dbname = 'mydatabase';  // Database name
$conn = mysqli_connect($host, $user, $password, $dbname);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

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

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

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

mysqli_close($conn);
?>
        

This complete example demonstrates how to use cURL to fetch a sentence from the API and save it to a MySQL database. You can adjust the SQL query to store the data in a different table or field as required.

5. Conclusion

This article explained how to use PHP to connect to the Baidu Wenxin Yiyan API, retrieve specific types of sentences, and save them to a MySQL database using cURL and JSON parsing. We hope this guide helps you implement API data fetching and processing quickly.