首页 >后端开发 >php教程 >PHP调用科大讯飞语音服务

PHP调用科大讯飞语音服务

不言
不言原创
2018-04-14 10:39:086713浏览

本篇文章给大家分享的内容是关于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 '语音合成成功,请查看文件!';
    }
}

语音合成示例:

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

相关推荐:

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

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


以上是PHP调用科大讯飞语音服务 的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn