Current Location: Home> Latest Articles> How to Integrate Baidu Voice Wake-up API with PHP: Detailed Steps and Precautions

How to Integrate Baidu Voice Wake-up API with PHP: Detailed Steps and Precautions

M66 2025-07-15

How to Integrate Baidu Voice Wake-up API with PHP: Detailed Steps and Precautions

With the rapid development of artificial intelligence technology, speech recognition and voice interaction have become indispensable parts of modern applications. Baidu provides a powerful voice wake-up API, which allows developers to easily integrate voice wake-up functionality. In this article, we will guide you on how to integrate Baidu's Voice Wake-up API using PHP, as well as share some tips and precautions.

Preparing for Baidu Voice Wake-up API

First, you need to register on Baidu's open platform and create an application. During the creation process, select the option to use the Voice Wake-up API, and you will receive an API Key and a Secret Key, which are essential for API calls.

PHP Code Implementation

We will use PHP's cURL extension to send HTTP requests and receive responses from the Baidu Voice Wake-up API. Below is an example code:


<?php
$url = 'https://vop.baidu.com/server_api'; // API URL
$apiKey = 'your_api_key'; // Your API Key
$secretKey = 'your_secret_key'; // Your Secret Key

// Voice wake-up model type, default is 1536 (Mandarin search model)
$devPid = 1536;

// Build HTTP request parameters
$params = array(
    'token' => '', // If you have a token, fill it here
    'dev_pid' => $devPid
);

// Calculate the signature
$authParams = http_build_query($params);
$sign = base64_encode(md5($authParams . $secretKey, true));

// Build the complete request URL
$requestUrl = $url . '?' . http_build_query($params) . '&sign=' . urlencode($sign);

// Send HTTP request
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

// Handle the API response
$result = json_decode($response, true);
if ($result && isset($result['err_no']) && $result['err_no'] == 0) {
    echo 'Wake-up successful!';
} else {
    if ($result && isset($result['err_msg'])) {
        echo 'Wake-up failed, error message: ' . $result['err_msg'];
    } else {
        echo 'Wake-up failed, unknown error';
    }
}
?>

Please make sure to replace 'your_api_key' and 'your_secret_key' in the code with your actual API Key and Secret Key that you obtain from the Baidu open platform.

Precautions

When integrating the Baidu Voice Wake-up API, here are a few important considerations:

  • Request the proper permissions: Ensure that you select the necessary permissions for using the Voice Wake-up API when creating your application.
  • Select the appropriate dev_pid: The dev_pid is the parameter for the voice model type. By default, it is 1536 (Mandarin search model). Depending on your actual needs, you may choose a different model.
  • Parameter signing and encryption: When building the HTTP request, you must sign and encrypt the request parameters to ensure data security. The example code uses the MD5 hash and Base64 encoding to compute the signature.
  • Error handling: In processing the API response, check the 'err_no' field to determine if the request was successful. If the request fails, you can check the 'err_msg' field for specific error messages.

With the information and sample code provided in this article, you should be able to successfully integrate the Baidu Voice Wake-up API using PHP. We hope these tips and precautions are helpful in your development work.