Artificial Intelligence 5 min read

Integrating Baidu Text-to-Speech API with PHP

This tutorial demonstrates how to obtain Baidu TTS credentials, construct the required signature, send an HTTP request using PHP's cURL library, and save the returned audio data as an MP3 file, providing a complete code example for developers.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Integrating Baidu Text-to-Speech API with PHP

With the rapid development of artificial intelligence, speech synthesis has gained increasing attention, and Baidu's Text-to-Speech (TTS) API offers a powerful, versatile solution.

This article provides a step‑by‑step guide on how to integrate the Baidu TTS service using PHP, including a full code example.

Obtaining API credentials : Register on the Baidu Speech Open Platform, create an application, and retrieve the APP ID , API Key , and Secret Key . These three parameters are required to authenticate requests to the TTS endpoint.

Sending the request : In PHP, the cURL extension is used to build and send an HTTP POST request to http://tsn.baidu.com/text2audio . The request includes the text to be synthesized, language, voice settings, and the authentication signature generated from the API key, secret key, current time, and a random nonce.

<code>&lt;?php<br/>/**<br/> * Use PHP to call Baidu Text‑to‑Speech API<br/> */<br/><br/>// Baidu TTS parameters<br/>$appId = 'your_app_id';<br/>$apiKey = 'your_api_key';<br/>$secretKey = 'your_secret_key';<br/>$text = '欢迎使用百度语音合成接口';<br/><br/>// Generate signature<br/>$curTime = time();<br/>$salt = mt_rand();<br/>$sign = md5($apiKey . $text . $salt . $curTime . $secretKey);<br/><br/>// Build request parameters<br/>$param = array(<br/>    'tex' => urlencode($text),<br/>    'lan' => 'zh',<br/>    'cuid' => 'your_unique_id',<br/>    'ctp' => '1',<br/>    'pit' => '8',<br/>    'spd' => '5',<br/>    'vol' => '5',<br/>    'per' => '0',<br/>    'aue' => '3',<br/>    'url' => 'http://www.example.com/callback.php', // optional callback<br/>    'callback' => 'http://www.example.com/callback.php', // optional callback<br/>    'cuid' => 'your_unique_id' // optional user ID<br/>);<br/><br/>// Send HTTP request<br/>$ch = curl_init();<br/>curl_setopt($ch, CURLOPT_URL, 'http://tsn.baidu.com/text2audio');<br/>curl_setopt($ch, CURLOPT_POST, 1);<br/>curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));<br/>curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br/>curl_setopt($ch, CURLOPT_HTTPHEADER, array(<br/>    'Content-Type: application/x-www-form-urlencoded',<br/>    'Content-Length: ' . strlen(http_build_query($param)),<br/>    'CurTime: ' . $curTime,<br/>    'Nonce: ' . $salt,<br/>    'CheckSum: ' . $sign<br/>));<br/><br/>$result = curl_exec($ch);<br/>curl_close($ch);<br/><br/>// Process response<br/>if ($result) {<br/>    // Save audio file<br/>    file_put_contents('output.mp3', $result);<br/>    echo '成功生成语音文件';<br/>} else {<br/>    echo '请求失败';<br/>}<br/>?&gt;</code>

Save the above script as a PHP file and execute it; if successful, an output.mp3 file containing the synthesized speech will appear in the current directory.

Remember to replace the placeholder values your_app_id , your_api_key , and your_secret_key with the actual credentials obtained from the Baidu platform.

Summary : By generating the required signature and posting the appropriate parameters via cURL, developers can call Baidu's TTS service from PHP, receive the audio data, and store it as an MP3 file, with the possibility to adapt the code for more complex scenarios.

PHPcurlSpeech Synthesistext-to-speechAPI IntegrationBaidu TTS
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.