PHP 음성을 계정 API 인터페이스로 구현하는 방법
1. 준비
iFlytek 계정 신청 http://www.xfyun.cn/
IP 화이트리스트 추가(5-10) 몇 분 안에 적용됨) APPID 및 APPKEY를 얻을 수 있는 오디오 파일(wav 또는 pcm 형식)을 준비합니다(서비스마다 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 음성을 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!