PHP呼叫科大訊飛語音服務

不言
發布: 2023-03-23 19:44:01
原創
6611 人瀏覽過

這篇文章給大家分享的內容是關於PHP呼叫科大訊飛語音服務,有著一定的參考價值,有需要的朋友可以參考一下

PHP呼叫科大訊飛語音服務

最近在做微信小程序,需要做語音識別,選擇了國內很有名的訊飛語音。
我的後台是PHP,在接入過程中走了一些坑,在這裡分享出來希望可以幫助需要的朋友


準備工作

  1. 申請訊飛帳號http://www.xfyun.cn/
    PHP呼叫科大訊飛語音服務

  2. ##加入IP白名單(5-10分鐘生效)

  3. 準備一個音訊檔案(wav或pcm格式)

  4. 取得APPID和APPKEY(

    每個服務的APPKEY不同

  5. 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呼叫京東聯盟開普勒、宙斯API模板

以上是PHP呼叫科大訊飛語音服務的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!