教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php语音到账api接口如何实现

php语音到账api接口如何实现

发布时间:2020-06-26   编辑:jiaochengji.com
教程集为您提供php语音到账api接口如何实现等资源,欢迎您收藏本站,我们将为您提供最新的php语音到账api接口如何实现资源

php语音到账api接口如何实现

1.准备工作
申请讯飞帐号http://www.xfyun.cn/

1.jpg

添加IP白名单(5-10分钟生效)准备一个音频文件(wav或pcm格式)获取APPID和APPKEY(每个服务的APPKEY不同)

const APP_ID = ‘xxxx’;
const APP_KEY_IAT = ‘xxxx’; //语音听写APPKEY
const APP_KEY_ISE = ‘xxxx’; //语音评测APPKEY
const 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接口如何实现的详细内容,更多请关注教程集其它相关文章!

  • 本文原创发布教程集,转载请注明出处,感谢您的尊重!
  • 您可能感兴趣的文章:
    php语音到账api接口如何实现
    手机QQ音乐如何已经登录的账号
    微信公众平台PHP开发版本例子
    中文语音识别技术在c#中的应用(二)
    USDT PHP开发包OmniTool简介
    php根据ip调用新浪api获取城市名转拼音
    HTML5实现微信jssdk录音播放语音的实例
    详解HTML5 录音遇到的坑
    12306身份证待核验是什么意思?怎么解决?
    Derek解读Bytom源码-Api Server接口服务

    [关闭]
    ~ ~