腾讯云ASR产品-PHP实现录音文件识别极速版鉴权请求

一、准备工作

(1)开通腾讯云 https://cloud.tencent.com/

(2)腾讯云控制台开通实时语音权限 https://console.cloud.tencent.com/asr

(3)控制台设置秘钥 https://console.cloud.tencent.com/cam/capi

内容

说明

支持语言

中文普通话

音频格式

wav、pcm、ogg-opus、speex、silk、mp3、m4a、aac

使用限制

支持100MB以内音频文件的识别

请求协议

HTTPS

请求地址

https://asr.cloud.tencent.com/asr/flash/v1/<appid>?{请求参数}

二、代码

代码语言:javascript
复制
<?php

//极速版录音文件识别
class SpeedVoice
{
//腾讯云密钥信息 需要配置
const APPID = "您的APPID";
const SECRET_ID = "您的SECRET_ID";
const SECRET_KEY = "您的SECRET_KEY";

const AGREEMENT = &#34;https&#34;;
const VOICE_URL = &#34;asr.cloud.tencent.com/asr/flash/v1/&#34;;
const HTTPRequestMethod = &#34;POST&#34;;
//引擎模型类型。8k_zh:8k 中文普通话通用;16k_zh:16k 中文普通话通用;16k_en:16k 英语;16k_zh_video:16k 音视频领域。
static $ENGINE_MODEL_TYPE = &#39;16k_zh&#39;;

//结果返回方式 0:同步返回,拿到全部中间结果, or 1:尾包返回
static $RES_TYPE = 1;

// 支持 wav、pcm、ogg-opus、speex、silk、mp3、m4a、aac。
static $VOICE_FORMAT = &#39;mp3&#39;;

//是否开启说话人分离
static $SPEAKER_DIARIZATION = 0;
//后处理参数
static $FILTER_DIRTY = 0;
static $FILTER_MODAL = 0;
static $FILTER_PUNC = 0;
static $CONVERT_NUM_MODE = 1;
static $WORD_INFO = 0;
static $FIRST_CHANNEL_ONLY = 1;


public static function voice($pathFile)
{
    //get请求 设置url参数
    $timestamp = time();

    $httpUrlParams =
        [
            &#34;appid&#34; =&gt; self::APPID,
            &#34;secretid&#34; =&gt; self::SECRET_ID,
            &#34;engine_type&#34; =&gt; self::$ENGINE_MODEL_TYPE,
            &#34;voice_format&#34; =&gt; self::$VOICE_FORMAT,
            &#34;timestamp&#34; =&gt; $timestamp,
            &#34;speaker_diarization&#34;  =&gt; self::$SPEAKER_DIARIZATION,
            &#34;filter_dirty&#34;  =&gt; self::$FILTER_DIRTY,
            &#34;filter_modal&#34;  =&gt; self::$FILTER_MODAL,
            &#34;filter_punc&#34;   =&gt; self::$FILTER_PUNC,
            &#34;convert_num_mode&#34;  =&gt; self::$CONVERT_NUM_MODE,
            &#34;word_info&#34; =&gt; self::$WORD_INFO,
            &#34;first_channel_only&#34; =&gt; self::$FIRST_CHANNEL_ONLY
    ];
    //get请求url拼接
    $requestUrl = self::AGREEMENT . &#34;://&#34; . self::VOICE_URL . self::APPID . &#34;?&#34;;
    //剔除appid
    unset($httpUrlParams[&#34;appid&#34;]);
    //生成URL请求地址
    $requestUrl .= http_build_query($httpUrlParams);
    //鉴权
    $sign = self::getAuthorizationString($httpUrlParams);

    //分片数据包
    $sectionData = file_get_contents($pathFile);
    $headers = [
        &#39;Host: asr.cloud.tencent.com&#39;,
        &#39;Content-Type: application/octet-stream&#39;,
        &#39;Authorization: &#39; . $sign,
        &#39;Content-Length: &#39; . strlen($sectionData),
    ];
    $result = self::get_curl_request($requestUrl, $sectionData, &#39;POST&#39;, $headers);
    print_r($result);
}


/**
 * 发送请求
 * @param $url
 * @param array $param
 * @param string $mothod
 * @param array $headers
 * @param int $return_status
 * @param int $flag 关闭https证书
 * @return array|bool|string
 */
static private function get_curl_request($url, $param, $mothod = &#39;POST&#39;, $headers = [], $return_status = 0, $flag = 0)
{
    $ch = curl_init();
    if (!$flag) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    }

    curl_setopt($ch, CURLOPT_TIMEOUT, 60);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if (strtolower($mothod) == &#39;post&#39;) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
    } else {
        $url = $url . &#34;?&#34; . http_build_query($param);
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 2);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $ret = curl_exec($ch);
    $code = curl_getinfo($ch);
    curl_close($ch);
    if ($return_status == &#34;1&#34;) {
        return array($ret, $code);
    }
    return $ret;
}

/**
 * 生成随机字符串
 * @param $len
 * @param bool $special 是否开启特殊字符
 * @return string
 */
private static function getRandomString($len, $special = false)
{
    $chars = array(
        &#34;a&#34;, &#34;b&#34;, &#34;c&#34;, &#34;d&#34;, &#34;e&#34;, &#34;f&#34;, &#34;g&#34;, &#34;h&#34;, &#34;i&#34;, &#34;j&#34;, &#34;k&#34;,
        &#34;l&#34;, &#34;m&#34;, &#34;n&#34;, &#34;o&#34;, &#34;p&#34;, &#34;q&#34;, &#34;r&#34;, &#34;s&#34;, &#34;t&#34;, &#34;u&#34;, &#34;v&#34;,
        &#34;w&#34;, &#34;x&#34;, &#34;y&#34;, &#34;z&#34;, &#34;A&#34;, &#34;B&#34;, &#34;C&#34;, &#34;D&#34;, &#34;E&#34;, &#34;F&#34;, &#34;G&#34;,
        &#34;H&#34;, &#34;I&#34;, &#34;J&#34;, &#34;K&#34;, &#34;L&#34;, &#34;M&#34;, &#34;N&#34;, &#34;O&#34;, &#34;P&#34;, &#34;Q&#34;, &#34;R&#34;,
        &#34;S&#34;, &#34;T&#34;, &#34;U&#34;, &#34;V&#34;, &#34;W&#34;, &#34;X&#34;, &#34;Y&#34;, &#34;Z&#34;, &#34;0&#34;, &#34;1&#34;, &#34;2&#34;,
        &#34;3&#34;, &#34;4&#34;, &#34;5&#34;, &#34;6&#34;, &#34;7&#34;, &#34;8&#34;, &#34;9&#34;
    );

    if ($special) {
        $chars = array_merge($chars, array(
            &#34;!&#34;, &#34;@&#34;, &#34;#&#34;, &#34;$&#34;, &#34;?&#34;, &#34;|&#34;, &#34;{&#34;, &#34;/&#34;, &#34;:&#34;, &#34;;&#34;,
            &#34;%&#34;, &#34;^&#34;, &#34;&amp;&#34;, &#34;*&#34;, &#34;(&#34;, &#34;)&#34;, &#34;-&#34;, &#34;_&#34;, &#34;[&#34;, &#34;]&#34;,
            &#34;}&#34;, &#34;&lt;&#34;, &#34;&gt;&#34;, &#34;~&#34;, &#34;+&#34;, &#34;=&#34;, &#34;,&#34;, &#34;.&#34;
        ));
    }

    $charsLen = count($chars) - 1;
    shuffle($chars);                            //打乱数组顺序
    $str = &#39;&#39;;
    for ($i = 0; $i &lt; $len; $i++) {
        $str .= $chars[mt_rand(0, $charsLen)];    //随机取出一位
    }
    return $str;
}

/**
 * 创建签名
 * @param $params array 提交参数数组
 * @return string
 */
private static function getAuthorizationString($params)
{
    //加密字符串拼接
    $signString = self::HTTPRequestMethod . self::VOICE_URL . self::APPID . &#34;?&#34;;
    //排序
    ksort($params, SORT_STRING);
    //去除appid
    unset($params[&#34;appid&#34;]);
    //转url
    $signString .= http_build_query($params);
    $sign = base64_encode(hash_hmac(&#39;SHA1&#39;, $signString, self::SECRET_KEY, true));
    return $sign;
}

}
//请求示例
SpeedVoice::$VOICE_FORMAT = "wav";
SpeedVoice::voice("./0914.wav");