• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP调用科大讯飞语音服务

php 搞代码 4年前 (2022-01-22) 22次浏览 已收录 0个评论

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

PHP调用科大讯飞语音服务

最近在做微信小程序,需要做语音识别,选择了国内很有名的讯飞语音。
我的后台是PHP,在接入过程中走了一些坑,在这里分享出来希望可以帮助需要的朋友

准备工作

  1. 申请讯飞帐号http://www.xfyun.cn/

  2. 添加IP白名单(5-10分钟生效)

  3. 准备一个音频文件(wav或pcm格式)

  4. 获取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 '语音合成成功,请查看文<i style="color:transparent">本¥文来源gaodai$ma#com搞$代*码*网(</i><strong>搞代gaodaima码</strong>件!';    }}

语音合成示例:

echo voiceTts('科大讯飞真给力', 'a.wav');

相关推荐:

用php调用so库文件中的代码

PHP调用京东联盟开普勒、宙斯API模板

以上就是PHP调用科大讯飞语音服务 的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP调用科大讯飞语音服务

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址