Current Location: Home> Latest Articles> How to Use PHP to Integrate Baidu Smart Speaker API for Voice Synthesis

How to Use PHP to Integrate Baidu Smart Speaker API for Voice Synthesis

gitbox 2025-06-15

1. Introduction to Smart Speaker API

Smart speakers are among the most popular smart home devices today and Baidu's smart speaker is one of the most representative products in this field. To better serve users, Baidu Cloud offers a Smart Speaker API that enables developers to create rich, personalized voice applications. The Smart Speaker API mainly provides features like speech synthesis, speech recognition, and wake-up functionality. This article will focus on how to integrate it using PHP.

2. Create a Baidu Cloud Account

2.1 Register Baidu Account

First, you need to register for a Baidu account and complete either phone or email verification. If you already have a Baidu account, you can skip this step.

2.2 Register for Baidu Cloud Account

Go to the Baidu Cloud official website and register for a smart cloud account. If you have already registered, you can directly log in.

3. Create an Application

Once logged in, go to the console, select "Application Management" from the left menu, and click the "Create Application" button. When creating an application, make sure to bind it to the appropriate smart device, such as a smart speaker.

4. Apply for API Key and Secret Key

After creating your application, you will need to apply for an API Key and Secret Key. This process is done via the "OAuth 2.0" authorization mechanism in the API management interface.

Click the "Create" button, fill in the required information (e.g., application name, services to be enabled), and submit the request. After completing this, your API Key will appear in the "Application List" section.

Similarly, you can create a Secret Key by clicking the "Create Secret Key" button.

5. PHP Code Example

Once you have the API Key and Secret Key, you can start integrating the API with PHP. Below is a simple code example:


// API Key and Secret Key
$api_key = "your_api_key";
$secret_key = "your_secret_key";
// API URL
$url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=" . $api_key . "&client_secret=" . $secret_key;
// Get Access Token
$res = file_get_contents($url);
$json = json_decode($res, true);
$access_token = $json['access_token'];
// Call the Speech Synthesis API
$text = "Welcome to Baidu Smart Speaker API";
$url = "http://tsn.baidu.com/text2audio?tex=" . urlencode($text) . "&lan=zh&cuid=xxxx&ctp=1&tok=" . $access_token;
header("Content-type:audio/mp3");
echo file_get_contents($url);

This code example demonstrates how to use the API Key and Secret Key for authorization, obtain an Access Token, and call the Speech Synthesis API to perform voice synthesis.

6. Conclusion

This article covers the steps to integrate Baidu Smart Speaker API using PHP, including registering a Baidu Cloud account, creating an application, and obtaining API Key and Secret Key. With these steps, you can enable voice synthesis and other functionalities to develop personalized voice applications for smart speakers. Additionally, pay attention to the validity period of the Access Token and renew it as necessary.

When using the API, it’s also crucial to handle authorization securely to avoid potential security risks.