中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

實現php語音到賬api接口的方法

發布時間:2020-08-19 14:41:32 來源:億速云 閱讀:285 作者:小新 欄目:編程語言

小編給大家分享一下實現php語音到賬api接口的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

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接口的方法有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

东至县| 紫云| 武胜县| 田林县| 阿城市| 奈曼旗| 卢氏县| 洞头县| 青州市| 瑞昌市| 客服| 龙口市| 东乡| 分宜县| 桂林市| 崇文区| 武穴市| 长宁县| 玉溪市| 神池县| 上高县| 绍兴县| 卫辉市| 黎川县| 永安市| 高邮市| 临朐县| 博乐市| 镇坪县| 楚雄市| 绵阳市| 青阳县| 彩票| 泌阳县| 长乐市| 木兰县| 绥阳县| 蒙山县| 临江市| 五峰| 弋阳县|