Current Location: Home> Latest Articles> How to Integrate Baidu Speech Recognition API with PHP - Step-by-Step Guide

How to Integrate Baidu Speech Recognition API with PHP - Step-by-Step Guide

M66 2025-06-12

How to Integrate Baidu Speech Recognition API with PHP

With the rapid development of artificial intelligence, speech recognition has become an important part of many daily applications. Baidu's Speech Recognition API provides developers with a powerful tool to convert speech into text, enabling more interactive features for applications. In this article, we will explain how to use PHP to integrate Baidu's Speech Recognition API, and we will provide relevant code examples to help developers quickly integrate this feature.

Step 1: Register for a Baidu Developer Account and Get API Key and Secret Key

Before you begin, you need to register for a Baidu developer account and create an application. Once your application is created, you will be provided with an API Key and a Secret Key, which are required for accessing the Baidu Speech Recognition API.

Step 2: Sending HTTP Requests Using PHP

Next, we need to use PHP's cURL library to send HTTP requests to call the Baidu Speech Recognition API. We will upload an audio file, send the data to the API, and retrieve the recognition result. Here's an example of PHP code:


<?php
// Set the request URL
$url = 'https://vop.baidu.com/server_api';

// Set the parameters
$params = array(
    'format' => 'pcm',        // Audio file format
    'dev_pid' => 1536,        // Language recognition type (Mandarin)
    'token' => 'YOUR_ACCESS_TOKEN',  // Access token
    'cuid' => 'YOUR_CUID',    // Client unique identifier
    'len' => filesize('path/to/your/audio/file.pcm'),  // Audio file size
    'speech' => base64_encode(file_get_contents('path/to/your/audio/file.pcm')), // Audio file content
);

// Set the request headers
$headers = array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen(json_encode($params)),
);

// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Send the HTTP request
$response = curl_exec($ch);

// Close cURL
curl_close($ch);

// Process the response
$result = json_decode($response, true);
if ($result && isset($result['result'])) {
    // Print the recognition result
    echo $result['result'][0];
} else {
    // Print the error message
    echo $result['err_msg'];
}
?>

Code Explanation

In the code above, we first set the request URL and provide the required parameters as an associative array. Then, we set the request headers, including Content-Type and Content-Length. Next, we initialize the cURL library and set the relevant options. Finally, we send the request and get the response.

Note that `YOUR_ACCESS_TOKEN` and `YOUR_CUID` need to be replaced with your own Baidu Speech Recognition API access token and client unique identifier. Additionally, `path/to/your/audio/file.pcm` should be replaced with the actual path to your audio file.

Conclusion

This concludes the basic steps and code example for integrating Baidu's Speech Recognition API with PHP. We hope this guide helps you quickly integrate speech recognition into your applications and improve the user experience. If you have any questions, feel free to leave a comment below.