Current Location: Home> Latest Articles> PHP Implementation of Baidu Wenxin Yiyan API Security Verification

PHP Implementation of Baidu Wenxin Yiyan API Security Verification

gitbox 2025-06-13

1. Introduction

Baidu Wenxin Yiyan API provides a rich collection of famous quotes and poetry, which can add a literary touch to your website. However, the online environment is full of uncertainties, so it's important to implement necessary security verifications to ensure the safety of API requests.

2. Security Verification

To ensure the security of the API interface, we usually employ a signature mechanism to verify if a request is legitimate. The basic process for implementing this security verification is as follows:

1. Sort all request parameters in dictionary order;

2. Concatenate the parameters in the format “parameter_name=parameter_value” and connect them with "&";

3. Append the Access Key Secret to the concatenated string and perform an MD5 calculation to generate a signature (sign);

4. Add the signature as a parameter in the request.

2.1 Encapsulated Code

We can use PHP to encapsulate a function to implement this security verification logic. Below is an example of the function `baidu_heart_words`, where `$params` are the request parameters, and `$accessKey` and `$accessKeySecret` are the API Access Key and Access Secret.


/**
 * Baidu Wenxin Yiyan API
 *
 * @param array $params Request parameters
 * @param string $accessKey Access Key
 * @param string $accessKeySecret Access Secret
 *
 * @return string Returns a quote
 */
function baidu_heart_words($params, $accessKey, $accessKeySecret)
{
    // Sort parameters in dictionary order
    ksort($params);
$query = http_build_query($params);

// Append accessKeySecret
$query .= $accessKeySecret;

// Calculate the MD5 value
$sign = md5($query);

// Add the signature to the request parameters
$params['sign'] = $sign;

// Send the request
$uri = 'https://api.xiaohuwei.cn/baidu_heart_words?' . http_build_query($params);
return file_get_contents($uri);

}

2.2 Usage Example

When using this encapsulated function, simply pass the request parameters, Access Key, and Access Secret, and the function will return a Baidu Wenxin Yiyan quote string, which can be displayed on your website. Below is an example:


$params = [
    'type' => '3',  // Famous quotes
    'c' => 'this-is-test',
    'f' => 'json',
    'temperature' => '0.6',
];
<p>$baidu_heart_words = baidu_heart_words($params, 'accessKey', 'accessKeySecret');<br>
echo $baidu_heart_words;<br>

The above code will return a JSON formatted string, such as:


{
    "errno": 0,
    "errmsg": "success",
    "data": {
        "id": "Dw3uaPUfoH",
        "source": "Sichuan",
        "author": "Lu Xun",
        "content": "The Catcher in the Rye — Dear Mr. Klaus"
    }
}

3. Conclusion

When using the Baidu Wenxin Yiyan API, ensuring the security of the interface is crucial. With the signature generation process and the encapsulated PHP function described in this article, you can effectively protect your API interface from unauthorized requests and attacks.