TTS
俗称 text to speech 。就是一个语音合成技术而已。可以将你发送来的文字装换成mp3文件。用途的话:暂时我用到的就只有一个银行系统的语音阀值异常报警系统了
废话不多说 直接贴代码了 相信各位小伙伴很简单找到规律
<?php
header('Content-Type:text/html;charset=UTF-8');
//$data=isset($GLOBALS['HTTP_RAW_POST_DATA'])?$GLOBALS['HTTP_RAW_POST_DATA']:(isset($_GET['data'])?$_GET['data']:'');
//$data = '{"country":"ZW","message":"你好"}';
$data = '{"country":"EN","message":"hello world"}';
$url_begin = 'https://ss0.baidu.com/6KAZsjip0QIZ8tyhnq/text2audio?tex=';
$url_end = '&cuid=dict&lan=ZH&ctp=1&pdt=30&vol=9&spd=4';
function str_split_unicode($str, $l = 0) {''
if ($l > 0) {
$ret = array();
$len = mb_strlen($str, "UTF-8");
for ($i = 0; $i < $len; $i += $l) {
$ret[] = mb_substr($str, $i, $l, "UTF-8");
}
return $ret;
}
return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
}
if ($data) {
try {
$post_json_data=json_decode($data, TRUE);
} catch (Exception $e) {
$post_json_data=NULL;
echo "please input effective data";
}
$country = $post_json_data['country'] ;
if($country=='ZW'){
$message = $post_json_data['message'];
$array = str_split_unicode($message);
$url_midst = '';
for($index=0;$index<count($array);$index++)
{
$url_midst = $url_midst.$array[$index].'%28%29';
}
$real_url = $url_begin.$url_midst.$url_end;
file_put_contents('///use/'.$message.'.mp3', file_get_contents($real_url));
echo $real_url;
}else{
$message = $post_json_data['message'];
//这里得加一段。将获得的message转发给另外一个接口。获取音标。
$array = explode(" ", $message);
$url_midst = '';
for($index=0;$index<count($array);$index++)
{
$url_midst = $url_midst.'%28'.$array[$index].'%29';
}
$real_url = $url_begin.$url_midst.$url_end;
file_put_contents('///use/'.$message.'.mp3', file_get_contents($real_url));
echo $real_url;
}
} else {
echo "please input effective data";
}
?>
代码已贴,相信熟悉php的小伙伴们已经看出来了规律。就是一条url地址+发现参数的眼睛而已~
url:
https://ss0.baidu.com/6KAZsjip0QIZ8tyhnq/text2audio?tex=%28go%29%28to%29&cuid=dict&lan=ZH&ctp=1&pdt=30&vol=9&spd=4
其中的go to 是你需要转换成音频的文字。而lan参数则是语种。vol是声音大小,spd是语音播放速度~ 至于其他的 就没有去尝试了 感兴趣的小伙伴可以去试试~