首先,要获得token
https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=APPKEY&client_secret=SecretKeyAPPKEY和Secret Key根据自己实际账号信息来填写,然后GET就获得了token和过期时间等信息,json格式。
然后就是语音合成了。
把文字转为语音,请求成功后百度会下行mp3文件。
$text = urlencode("早上好!"); //文字
$token =""; //openID的token
$text = "tex=".$text."&lan=zh&cuid=000000&ctp=1&tok=$token"; //cuid为设备唯一标识,暂统一设为000000
$url = "http://tsn.baidu.com/text2audio?$text";
getVoice($url);
function getVoice($url){
$filename = time()+rand(100,999).".mp3"; //文件名,时间戳+三位随机数
ob_start();
readfile($url);
$voice = ob_get_contents();
ob_end_clean();
$file = @fopen($filename,"a");
fwrite($file,$voice);
fclose($file);
return $filename;
}mp3文件就保存到本地文件夹了。

本文介绍了如何利用百度开放平台的API实现从文字到语音的转换过程。首先通过获取访问令牌(token),接着发送包含待转换文字信息的请求,最终接收并保存由百度返回的MP3格式语音文件。

2万+

被折叠的 条评论
为什么被折叠?



