百度语音开发api:文字转语音接口

首先,要得到tokenphp

https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=APPKEY&client_secret=SecretKey
APPKEY和Secret Key根据本身实际帐号信息来填写,而后GET就得到了token和过时时间等信息,json格式。

而后就是语音合成了。json

把文字转为语音,请求成功后百度会下行mp3文件。api

$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文件就保存到本地文件夹了。