Speech recognition has become an increasingly important feature in modern smart applications, especially in areas such as voice assistants, smart customer service, and voice input. Baidu’s speech recognition API provides developers with a powerful and stable voice-to-text solution. This guide walks you through how to implement it using PHP.
The Baidu API supports converting audio into text, with multilingual support and various input formats. Developers can send audio data to the API via HTTP requests and receive the transcribed result in JSON format, which can then be processed in their applications.
To successfully use the API, you need to configure the request parameters correctly and parse the JSON response to extract the recognized text.
Before calling the API, you must register an account on Baidu Cloud, create an application, and obtain the following credentials:
These are essential for authentication and must be included in your requests. Also, make sure that speech recognition services are enabled for your application.
// Set Baidu speech recognition API URL
$url = 'http://vop.baidu.com/server_api';
// Required parameters
$cuid = "123456789"; // Unique user ID
$format = "pcm"; // Audio format
$rate = 16000; // Sample rate
$channel = 1; // Channel count
$token = "24.f601973d83600bb9532f8c32ed61c45c.2592000.1570309632.282335-17098763"; // Access token
// Read audio file contents
$audio = file_get_contents("test.pcm");
// Set request headers
$header = array(
"Content-Type: audio/" . $format,
"Content-Length: " . strlen($audio),
"cuid: " . $cuid,
"rate: " . $rate,
"channel: " . $channel,
"token: " . $token
);
$options = array(
'http' => array(
'method' => 'POST',
'header' => implode("\r\n", $header),
'content' => $audio
)
);
$context = stream_context_create($options);
// Send request and parse JSON response
$result = file_get_contents($url, false, $context);
$result = json_decode($result, true);
The code can be broken down into three steps:
Be aware that the access_token is essential for authentication and must be valid and active. You can refer to the official Baidu documentation for how to generate this token.
This article explained how to implement speech recognition in PHP using Baidu’s API, including how to configure parameters, upload audio data, and handle the results. With proper integration, you can easily add speech-to-text functionality to your application and enhance user experience with voice capabilities.