PHP appelle le service vocal iFlytek

不言
Libérer: 2023-03-23 19:44:01
original
6611 Les gens l'ont consulté

Le contenu partagé avec vous dans cet article concerne PHP appelant le service vocal iFlytek. Il a une certaine valeur de référence. Les amis dans le besoin peuvent s'y référer

PHP appelant le service vocal iFlytek

Récemment, je travaille sur une applet WeChat et j'ai besoin de faire de la reconnaissance vocale, j'ai donc choisi iFlytek, un service de reconnaissance vocale national bien connu.
Mon expérience est PHP et j'ai traversé quelques pièges lors du processus d'accès. Je le partage ici dans l'espoir que cela puisse aider des amis dans le besoin


Préparation

  1. Demander un compte iFlytek http://www.xfyun.cn/


    PHP appelle le service vocal iFlytek

  2. Ajouter Liste blanche IP (Effective dans 5-10 minutes)

  3. Préparez un fichier audio (format wav ou pcm)

  4. Obtenez APPID et APPKEY (

    chacun L'APPKEY de chaque service est différent)

const APP_ID = 'xxxx';const APP_KEY_IAT = 'xxxx'; //语音听写APPKEYconst APP_KEY_ISE = 'xxxx'; //语音评测APPKEYconst APP_KEY_TTS = 'xxxx'; //语音合成APPKEY
Copier après la connexion

Dictée vocale

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;
}
Copier après la connexion

Exemple de dictée vocale :

voiceIat('a.wav');
Copier après la connexion

Évaluation vocale

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;
}
Copier après la connexion

Exemple d'évaluation vocale :

echo voiceIse('a.wav', '科大讯飞真给力');
Copier après la connexion

Synthèse vocale

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 '语音合成成功,请查看文件!';
    }
}
Copier après la connexion

Exemple de synthèse vocale :

echo voiceTts('科大讯飞真给力', 'a.wav');
Copier après la connexion
Recommandations associées :

Utilisez php pour appeler le code dans la bibliothèque so fichier

PHP appelle les modèles d'API JD Alliance Kepler et Zeus


Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
php
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!