PHP가 iFlytek 음성 서비스를 호출합니다.

不言
풀어 주다: 2023-03-23 19:44:01
원래의
6611명이 탐색했습니다.

이 기사에서 공유한 내용은 iFlytek 음성 서비스를 호출하는 PHP에 관한 것입니다. 이는 특정 참조 가치가 있습니다. 도움이 필요한 친구는 이를 참조할 수 있습니다.

PHP가 iFlytek 음성 서비스를 호출합니다.

현재 WeChat 애플릿을 작업 중입니다. , 음성인식을 해야 해서 중국에서 잘 알려진 아이플라이텍 보이스(iFlytek Voice)를 선택했습니다.
내 배경은 PHP이고, 액세스 과정에서 몇 가지 함정을 겪었습니다. 도움이 필요한 친구들에게 도움이 되기를 바라며 여기에 공유합니다


Preparation

  1. iFlytek 계정 http: //www.xfyun.cn/
    PHP가 iFlytek 음성 서비스를 호출합니다.

  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를 사용하여 JD Alliance Kepler 및 Zeus API 템플릿 호출


위 내용은 PHP가 iFlytek 음성 서비스를 호출합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
php
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!