• 技术文章 >后端开发 >PHP问题

    php语音到账api接口如何实现

    angryTomangryTom2019-10-26 15:51:05原创794

    php语音到账api接口如何实现

    1.准备工作
    申请讯飞帐号http://www.xfyun.cn/

    1.jpg

    添加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相关知识,请访问PHP中文网

    以上就是php语音到账api接口如何实现的详细内容,更多请关注php中文网其它相关文章!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:php 语音
    上一篇:php随机背景图怎么表示 下一篇:php语句包含中文无法查询
    大前端线上培训班

    相关文章推荐

    • PHP调用科大讯飞语音服务 • PHP在线语音合成• PHP生成语音 • 语音转换文字怎么弄

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网