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.
Before starting the integration, complete the following preparations:
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.
After obtaining the Access Token, upload a wake-up word file and use the API for voice wake-up:
<?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.
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.