![af07deb3a9e25b75470e7c46fe933749.png](https://i-blog.csdnimg.cn/blog_migrate/39315381b04acc866fe5652c5a16940d.png)
首先在百度云要先创建应用,获取apiKey 、 secretKey、 百度云账号的用户id
php 调用接口:
//百度语音合成
public function voice_st(){
$text = '你怎么这么好看';//输入的文字,必须小于4096字节
$cuid = '你的用户id';//用户id,唯一标识
$apiKey = '你的apiKey';
$secretKey = '你的secretKey';
if(!session('voice_st_token')){
$url = 'https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id='.$apiKey.'&client_secret='.$secretKey;
$oauth = $this->getJson($url);
session(['voice_st_token'=>$oauth['access_token'], 'expire'=>2592000]);// token 缓存时间30天
}
$token = session('voice_st_token');
$tex = urlencode(urlencode($text));
$mp3_url = 'http://tsn.baidu.com/text2audio?lan=zh&ctp=1&cuid='.$cuid.'&tok='.$token.'&tex='.$tex.'&vol=9&per=3&spd=5&pit=5&aue=3';
header("Location:".$mp3_url);
}
private function getJson($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}
在浏览器中访问voice_st 方法,即可看到转化好的MP3:
![ea6a8657a973987f256c90192d53d472.png](https://i-blog.csdnimg.cn/blog_migrate/b6b1e8521278031a10ddad947fdfa948.png)
百度语音合成接口文档地址:https://cloud.baidu.com/doc/SPEECH/s/njwvy50yl