Current Location: Home> Latest Articles> Comprehensive Guide to Integrating Baidu Voice Wake-Up API with PHP

Comprehensive Guide to Integrating Baidu Voice Wake-Up API with PHP

M66 2025-09-16

Complete Guide to Integrating Baidu Voice Wake-Up API with PHP

Voice technology is increasingly applied in modern society. The Baidu Voice Wake-Up API is a powerful speech recognition tool that supports custom wake-up words, allowing users to interact with applications via voice. This article provides a step-by-step guide to integrating the Baidu Voice Wake-Up API using PHP, with ready-to-use code examples.

Preparation

Before starting the integration, complete the following preparations:

  • Register a Baidu developer account: Sign up on the Baidu Open Platform, create an application, and obtain an API Key and Secret Key.
  • Install the PHP environment: Make sure PHP is installed and the cURL extension is enabled.

Obtaining an Access Token

Before using the Baidu Voice Wake-Up API, you need to obtain an Access Token. The example below demonstrates how to get the token:

<?php
$clientId = 'your_client_id';
$clientSecret = 'your_client_secret';

$url = 'https://aip.baidubce.com/oauth/2.0/token';
$data = array(
    'grant_type' => 'client_credentials',
    'client_id' => $clientId,
    'client_secret' => $clientSecret
);

$options = array(
    'http' => array(
        'method' => 'POST',
        'header' => 'Content-Type: application/x-www-form-urlencoded',
        'content' => http_build_query($data),
    ),
);

$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
$result = json_decode($response, true);

$accessToken = $result['access_token'];
?>

Replace $clientId and $clientSecret with your actual values from the Baidu Open Platform.

Performing Voice Wake-Up

After obtaining the Access Token, upload a wake-up word file and use the API for voice wake-up:

  • Upload wake-up word file: Create and upload a wake-up word file on the Baidu Open Platform, and obtain the file ID.
  • Call the voice wake-up API: Use the following code along with the Access Token and wake-up word file ID.
<?php
$accessToken = 'your_access_token';
$deviceId = 'your_device_id';
$wordListId = 'your_word_list_id';

$url = 'https://vop.baidu.com/server_api';
$data = array(
    'access_token' => $accessToken,
    'device_id' => $deviceId,
    'wordlist_id' => $wordListId,
);

$options = array(
    'http' => array(
        'header' => 'Content-Type: application/json',
        'method' => 'POST',
        'content' => json_encode($data),
    ),
);

$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
$result = json_decode($response, true);

if ($result['err_no'] == 0) {
    // Voice wake-up successful
} else {
    // Voice wake-up failed
}
?>

Replace $accessToken with the token obtained earlier, and $deviceId and $wordListId with the actual device ID and wake-up word file ID.

Development Tips

  • Ensure the server can access Baidu servers. Configure firewalls or network restrictions as needed.
  • Access Tokens have expiration times. It is recommended to refresh the token before each request to avoid failures.
  • Carefully read the Baidu Voice Wake-Up API documentation to understand parameters and usage.
  • For stability and security, validate request parameters and handle errors appropriately.

Conclusion

This article introduced the complete process of integrating the Baidu Voice Wake-Up API using PHP, including obtaining an Access Token, uploading wake-up word files, and calling the API for voice wake-up. With proper network configuration and error handling, developers can reliably implement voice wake-up functionality to enhance user interaction.