Current Location: Home> Latest Articles> PHP Code Implementation of Baidu Wenxin Yiyan API for Scheduled Task Management and Execution

PHP Code Implementation of Baidu Wenxin Yiyan API for Scheduled Task Management and Execution

gitbox 2025-06-18

1. Understanding Baidu Wenxin Yiyan API

The Baidu Wenxin Yiyan API is widely used by developers to fetch random motivational or inspirational quotes. It provides an API that returns a random sentence, making it ideal for use as a quote or motivational statement at the footer of blogs or websites.

1.1. How to Use the API

To use this API, simply send a GET request to the following URL to retrieve a random quote:

https://v1.alapi.cn/api/hitokoto?format=json

The API returns data in JSON format, like this:

{
        "status": true,
        "data": {
            "id": "7742",
            "hitokoto": "I've walked far into this dead-end alley of life, and now I can only walk downhill.",
            "type": "a",
            "from": "The Disappearance of Haruhi Suzumiya",
            "from_who": "Yuki Nagato",
            "creator": "XShow",
            "creator_uid": 2886,
            "reviewer": 0,
            "uuid": "9cd04227-aa14-4df9-b8e0-6c0a5e4c6627",
            "created_at": "1616677997"
        }
    }

1.2. API Parameters

The API provides the following optional parameters:

  • c: Category ID (1 for Anime, 2 for Comic, 3 for Game, 4 for Novel, 5 for Original, 6 for Internet, 7 for Other; defaults to random).
  • encode: Character encoding. Acceptable values are urlencode, base64; defaults to no encoding.
  • apikey: The API key (optional).

You can apply for an API key on the Baidu Wenxin API application page.

2. PHP Code Implementation

Here's an example of using PHP to call this API and retrieve a random quote:

<?php
// Function to get a random quote
function getHitokoto($c = '1', $encode = 'urlencode', $apiKey = '') {
    $url = "https://v1.alapi.cn/api/hitokoto?c={$c}&encode={$encode}&apikey={$apiKey}";
    $data = file_get_contents($url);
    return json_decode($data, true);
}

$hitokoto = getHitokoto();
echo $hitokoto['data']['hitokoto'];
?>

In the code above, we define a function getHitokoto to send a request to the API and retrieve a quote. After calling the function, we fetch the quote and display it on the page.

3. Scheduled Task Management and Execution

3.1. Scheduled Task Setup

Let’s look at how to use a PHP scheduled task manager to run the task every minute. We can use a CRON expression to set the schedule.

On a Linux system, you can edit the user’s scheduled tasks with the following command:

<span class="fun">crontab -e</span>

Then, you can set up a CRON expression to execute the task every minute:

<span class="fun">* * * * * curl https://example.com/hitokoto.php > /dev/null 2>&1</span>

In the above example, https://example.com/hitokoto.php is the actual URL of the PHP script you want to run.

3.2. PHP Code for Scheduled Task

Here's the PHP code for the scheduled task:

<?php
// Function to get a random quote
function getHitokoto($c = '1', $encode = 'urlencode', $apiKey = '') {
    $url = "https://v1.alapi.cn/api/hitokoto?c={$c}&encode={$encode}&apikey={$apiKey}";
    $data = file_get_contents($url);
    return json_decode($data, true);
}

// Get the quote and save it to a file
$hitokoto = getHitokoto();
file_put_contents('/var/www/html/wordpress/hitokoto.txt', $hitokoto['data']['hitokoto']);
echo "Done!";
?>

In this code, we first define the function getHitokoto to call the API and fetch the quote. We then save the quote to /var/www/html/wordpress/hitokoto.txt, which can be accessed later by your website. Finally, save this code on your server and set it to execute every minute using a CRON job.

4. Conclusion

This article covers the detailed steps of using PHP to integrate the Baidu Wenxin Yiyan API for scheduled task execution. By understanding the process, you can retrieve random quotes using the API and store them in a file or database. You can automate this process with a task scheduler, making it convenient and efficient.