重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

实现php语音到账api接口的方法

小编给大家分享一下实现php语音到账api接口的方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

创新互联长期为上1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为泉州企业提供专业的成都网站建设、做网站,泉州网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。

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

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

实现php语音到账api接口的方法

添加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语音到账api接口的方法有了一定的了解,想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


网站栏目:实现php语音到账api接口的方法
文章源于:http://cqcxhl.com/article/iieddo.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP