php语音到账api接口如何实现
1.准备工作
申请讯飞帐号http://www.xfyun.cn/
添加ip白名单(5-10分钟生效)准备一个音频文件(wav或pcm格式)获取appid和appkey(每个服务的appkey不同)
const app_id = ‘xxxx’;const app_key_iat = ‘xxxx’; //语音听写appkeyconst app_key_ise = ‘xxxx’; //语音评测appkeyconst app_key_tts = ‘xxxx’; //语音合成appkey
语音听写
public function voiceiat($file_path){$param = [‘engine_type’ => ‘sms16k’,‘aue’ => ‘raw’];$cur_time = (string)time();$x_param = base64_encode(json_encode($param));$header_data = [‘x-appid:’.self::app_id,‘x-curtime:’.$cur_time,‘x-param:’.$x_param,‘x-checksum:’.md5(self::app_key_iat.$cur_time.$x_param),‘content-type:application/x-www-form-urlencoded; charset=utf-8’];//body$file_path = $file_path;$file_content = file_get_contents($file_path);$body_data = ‘audio=’.urlencode(base64_encode($file_content));//request$url = “http://api.xfyun.cn/v1/service/v1/iat”;$ch = curl_init();curl_setopt($ch, curlopt_url, $url);curl_setopt($ch, curlopt_header, 0);curl_setopt($ch, curlopt_returntransfer, true);curl_setopt($ch, curlopt_post, true);curl_setopt($ch, curlopt_httpheader, $header_data);curl_setopt($ch, curlopt_postfields, $body_data);$result = curl_exec($ch);curl_close($ch);return $result;}
语音听写示例:
voiceiat('a.wav');
语音评测
public function voiceise($file_path, $content){$param = [‘language’ => ‘cn’,‘aue’ => ‘raw’,‘category’ => ‘read_sentence’];$cur_time = (string)time();$x_param = base64_encode(json_encode($param));$header_data = [‘x-appid:’.self::app_id,‘x-curtime:’.$cur_time,‘x-param:’.$x_param,‘x-checksum:’.md5(self::app_key_ise.$cur_time.$x_param),‘content-type:application/x-www-form-urlencoded; charset=utf-8’];//body$file_path = $file_path;$file_content = file_get_contents($file_path);$body_data = ‘audio=’.urlencode(base64_encode($file_content)).’&text=’.urlencode($content);$url = “http://api.xfyun.cn/v1/service/v1/ise”;$ch = curl_init();curl_setopt($ch, curlopt_url, $url);curl_setopt($ch, curlopt_header, 0);curl_setopt($ch, curlopt_returntransfer, true);curl_setopt($ch, curlopt_post, true);curl_setopt($ch, curlopt_httpheader, $header_data);curl_setopt($ch, curlopt_postfields, $body_data);$result = curl_exec($ch);curl_close($ch);return $result;}
语音评测示例:
echo voiceise(‘a.wav’, ‘科大讯飞真给力’);
语音合成:
public function voicetts($content, $output_path){$param = [‘engine_type’ => ‘intp65’,‘auf’ => ‘audio/l16;rate=16000’,‘aue’ => ‘raw’,‘voice_name’ => ‘xiaoyan’,‘speed’ => ‘0’];$cur_time = (string)time();$x_param = base64_encode(json_encode($param));$header_data = [‘x-appid:’.self::app_id,‘x-curtime:’.$cur_time,‘x-param:’.$x_param,‘x-checksum:’.md5(self::app_key_tts.$cur_time.$x_param),‘content-type:application/x-www-form-urlencoded; charset=utf-8’];//body$body_data = ‘text=’.urlencode($content);//request$url = “http://api.xfyun.cn/v1/service/v1/tts”;$ch = curl_init();curl_setopt($ch, curlopt_url, $url);curl_setopt($ch, curlopt_header, true);curl_setopt($ch, curlopt_returntransfer, true);curl_setopt($ch, curlopt_post, true);curl_setopt($ch, curlopt_httpheader, $header_data);curl_setopt($ch, curlopt_postfields, $body_data);$result = curl_exec($ch);$res_header_size = curl_getinfo($ch, curlinfo_header_size);$res_header = substr($result, 0, $res_header_size);curl_close($ch);if(stripos($res_header, ‘content-type: audio/mpeg’) === false){ //合成错误return substr($result, $res_header_size);}else{file_put_contents($output_path, substr($result, $res_header_size));return ‘语音合成成功,请查看文件!’;}}
语音合成示例:
echo voicetts(‘科大讯飞真给力’, ‘a.wav’);
更多php相关知识,请访问!
以上就是php语音到账api接口如何实现的详细内容。