Current Location: Home> Latest Articles> How to Customize and Distribute Data from Baidu Wenxin Yiyan API in PHP Development

How to Customize and Distribute Data from Baidu Wenxin Yiyan API in PHP Development

gitbox 2025-06-17

What is Baidu Wenxin Yiyan API?

Baidu Wenxin Yiyan API is an API that provides random sentences in various categories, including animation, comics, games, novels, and original content. Each time the API is called, a random sentence is returned, suitable for use in personal blogs, websites, and other scenarios, offering a richer user experience.

How to Use Baidu Wenxin Yiyan API?

Step 1: Obtain the API Key

To use the Baidu Wenxin Yiyan API, you first need to apply for the API Key on Baidu's Open Cloud Platform. The specific steps are as follows:

  1. Visit the Baidu Open Cloud Platform website and register/login.
  2. Go to the console page, click on "Application Management—Create Application," and choose the Wenxin Yiyan API.
  3. In the API management page, copy your API Key.
$api_key = 'your_api_key';

Step 2: Call the API

After obtaining the API Key, you can use PHP code to call the API. The specific steps are as follows:

  1. Set the API request URL and parameters.
  2. Use the PHP function file_get_contents() to send a request to the API.
  3. Parse the returned data and extract the desired sentence.
$url = 'https://v1.hitokoto.cn';
$params = array(
    'c' => '',
    'encode' => 'json',
    'charset' => 'utf-8',
    'max_length' => 30
);

$response = file_get_contents($url . '?' . http_build_query($params));
$data = json_decode($response, true);

if (isset($data['hitokoto'])) {
    $sentence = $data['hitokoto'];
}

How to Customize and Distribute Data from Baidu Wenxin Yiyan API?

If you want to customize the sentences returned by the API or distribute the data to multiple sites, the following methods can help you achieve this.

Method 1: Customize Data Using API Parameters

Baidu Wenxin Yiyan API provides several parameters, allowing users to customize the returned data by specifying different parameters.

Parameter c

The parameter c specifies the category of the returned sentence, such as:

  • a - Animation
  • b - Comics
  • c - Games
  • d - Novels
  • e - Original

For example, if you want to get an animation sentence, you can set the parameter c to 'a':

$params = array(
    'c' => 'a',
    'encode' => 'json',
    'charset' => 'utf-8',
    'max_length' => 30
);

Parameter l

The parameter l specifies the length of the returned sentence, ranging from 1 to 200 characters. For example, if you want to get a sentence with a length of 20 characters, you can set the parameter l to 20:

$params = array(
    'c' => '',
    'encode' => 'json',
    'charset' => 'utf-8',
    'max_length' => 20
);

Method 2: Create a Custom API

You can also create a custom API based on your needs to achieve data customization and distribution. The steps are as follows:

  1. Create a PHP file that contains code to fetch data from Baidu Wenxin Yiyan API.
  2. Define your own API endpoint in the PHP file.
  3. Use this API on your website to fetch data.
if ($_GET['type'] == 'hitokoto') {
    $params = array(
        'c' => 'a',
        'encode' => 'json',
        'charset' => 'utf-8',
        'max_length' => 30
    );
    $url = 'https://v1.hitokoto.cn?' . http_build_query($params);
    $data = json_decode(file_get_contents($url), true);
    echo $data['hitokoto'];
}

For example, by visiting the following link, you can get an animation sentence:

http://your_site.com/your_api.php?type=hitokoto&c=a

Conclusion

By following the methods outlined above, you can customize and distribute data from the Baidu Wenxin Yiyan API, providing a more personalized experience for your users.