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.
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.
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.
When integrating the Baidu Voice Wake-up API, here are a few important considerations:
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.