php 讯飞语音评测_PHP调用科大讯飞语音服务

本文详细介绍了如何使用PHP调用科大讯飞的语音识别、评测和合成服务。通过实例代码展示了如何进行语音听写、评测和合成的操作,包括设置参数、构造请求和解析响应。对于需要在PHP后台实现语音功能的开发者,提供了宝贵的参考。
摘要由CSDN通过智能技术生成

本篇文章给大家分享的内容是关于PHP调用科大讯飞语音服务 ,有着一定的参考价值,有需要的朋友可以参考一下

PHP调用科大讯飞语音服务最近在做微信小程序,需要做语音识别,选择了国内很有名的讯飞语音。

我的后台是PHP,在接入过程中走了一些坑,在这里分享出来希望可以帮助需要的朋友

准备工作申请讯飞帐号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');

相关推荐:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值