当前位置:网站首页>Tencent cloud ASR product -php realizes the authentication request of the extremely fast version of recording file identification
Tencent cloud ASR product -php realizes the authentication request of the extremely fast version of recording file identification
2022-06-24 03:19:00 【Yuanlunqiao】
One 、 preparation
(1) Open Tencent cloud https://cloud.tencent.com/
(2) Tencent cloud console opens real-time voice permission https://console.cloud.tencent.com/asr
(3) The console sets the secret key https://console.cloud.tencent.com/cam/capi
Content | explain |
|---|---|
Support language | Mandarin Chinese |
Audio format | wav、pcm、ogg-opus、speex、silk、mp3、m4a、aac |
Usage restriction | Support 100MB Identification of internal audio files |
Request protocol | HTTPS |
Request address | https://asr.cloud.tencent.com/asr/flash/v1/<appid>?{ Request parameters } |
Two 、 Code
<?php
// Speed recording file recognition
class SpeedVoice
{
// Tencent cloud key information Need configuration
const APPID = " Your APPID";
const SECRET_ID = " Your SECRET_ID";
const SECRET_KEY = " Your SECRET_KEY";
const AGREEMENT = "https";
const VOICE_URL = "asr.cloud.tencent.com/asr/flash/v1/";
const HTTPRequestMethod = "POST";
// Engine model type .8k_zh:8k Mandarin Chinese is common ;16k_zh:16k Mandarin Chinese is common ;16k_en:16k English ;16k_zh_video:16k Audio and video .
static $ENGINE_MODEL_TYPE = '16k_zh';
// Result return method 0: Sync back , Get all the intermediate results , or 1: Tail package return
static $RES_TYPE = 1;
// Support wav、pcm、ogg-opus、speex、silk、mp3、m4a、aac.
static $VOICE_FORMAT = 'mp3';
// Whether to enable speaker separation
static $SPEAKER_DIARIZATION = 0;
// Post processing parameters
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 request Set up url Parameters
$timestamp = time();
$httpUrlParams =
[
"appid" => self::APPID,
"secretid" => self::SECRET_ID,
"engine_type" => self::$ENGINE_MODEL_TYPE,
"voice_format" => self::$VOICE_FORMAT,
"timestamp" => $timestamp,
"speaker_diarization" => self::$SPEAKER_DIARIZATION,
"filter_dirty" => self::$FILTER_DIRTY,
"filter_modal" => self::$FILTER_MODAL,
"filter_punc" => self::$FILTER_PUNC,
"convert_num_mode" => self::$CONVERT_NUM_MODE,
"word_info" => self::$WORD_INFO,
"first_channel_only" => self::$FIRST_CHANNEL_ONLY
];
//get request url Splicing
$requestUrl = self::AGREEMENT . "://" . self::VOICE_URL . self::APPID . "?";
// To eliminate appid
unset($httpUrlParams["appid"]);
// Generate URL Request address
$requestUrl .= http_build_query($httpUrlParams);
// authentication
$sign = self::getAuthorizationString($httpUrlParams);
// Fragmented packet
$sectionData = file_get_contents($pathFile);
$headers = [
'Host: asr.cloud.tencent.com',
'Content-Type: application/octet-stream',
'Authorization: ' . $sign,
'Content-Length: ' . strlen($sectionData),
];
$result = self::get_curl_request($requestUrl, $sectionData, 'POST', $headers);
print_r($result);
}
/**
* Send a request
* @param $url
* @param array $param
* @param string $mothod
* @param array $headers
* @param int $return_status
* @param int $flag close https certificate
* @return array|bool|string
*/
static private function get_curl_request($url, $param, $mothod = 'POST', $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) == 'post') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
} else {
$url = $url . "?" . 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 == "1") {
return array($ret, $code);
}
return $ret;
}
/**
* Generate random string
* @param $len
* @param bool $special Whether to open special characters
* @return string
*/
private static function getRandomString($len, $special = false)
{
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
if ($special) {
$chars = array_merge($chars, array(
"!", "@", "#", "$", "?", "|", "{", "/", ":", ";",
"%", "^", "&", "*", "(", ")", "-", "_", "[", "]",
"}", "<", ">", "~", "+", "=", ",", "."
));
}
$charsLen = count($chars) - 1;
shuffle($chars); // Disorder array order
$str = '';
for ($i = 0; $i < $len; $i++) {
$str .= $chars[mt_rand(0, $charsLen)]; // Take out one at random
}
return $str;
}
/**
* Create signature
* @param $params array Commit parameter array
* @return string
*/
private static function getAuthorizationString($params)
{
// Encrypted string concatenation
$signString = self::HTTPRequestMethod . self::VOICE_URL . self::APPID . "?";
// Sort
ksort($params, SORT_STRING);
// Remove appid
unset($params["appid"]);
// turn url
$signString .= http_build_query($params);
$sign = base64_encode(hash_hmac('SHA1', $signString, self::SECRET_KEY, true));
return $sign;
}
}
// Request example
SpeedVoice::$VOICE_FORMAT = "wav";
SpeedVoice::voice("./0914.wav");
边栏推荐
- New Google brain research: how does reinforcement learning learn to observe with sound?
- Ner's past, present and future Overview - past
- RI Geng series: write a simple shell script, but it seems to have technical content
- Windowsvpn client is coveted by vulnerabilities, 53% of companies face supply chain attacks | global network security hotspot
- How to check the progress of trademark registration? Where can I find it?
- JD Logistics: from giant baby to mainstay
- 2022-2028 global cancer biopsy instrument and kit industry research and trend analysis report
- How to register a trademark? Is the process troublesome?
- Tencent cloud launched its new 100g+ cloud server product!! Expect more than 400g+ in the future!
- Velocitytracker use
猜你喜欢

2022-2028 global marine clutch industry research and trend analysis report

The cost of on-site development of software talent outsourcing is higher than that of software project outsourcing. Why
![[51nod] 3216 Awards](/img/94/fdb32434d1343040d711c76568b281.jpg)
[51nod] 3216 Awards
![[51nod] 2102 or minus and](/img/68/0d966b0322ac1517dd2800234d386d.jpg)
[51nod] 2102 or minus and

2022-2028 global high tibial osteotomy plate industry research and trend analysis report
![[51nod] 3047 displacement operation](/img/cb/9380337adbc09c54a5b984cab7d3b8.jpg)
[51nod] 3047 displacement operation

2022-2028 global anti counterfeiting label industry research and trend analysis report

Simple and beautiful weather code

On Sunday, I rolled up the uni app "uview excellent UI framework"

UI automation based on Selenium
随机推荐
Micro build low code enterprise exchange day · Shenzhen station opens registration
How to check the progress of trademark registration? Where can I find it?
What aspects does the intelligent identification system include? Is the technology of intelligent identification system mature now?
QT creator tips
[summary of interview questions] zj6 redis
UI automation based on Selenium
How to build glasses website what are the functions of glasses website construction
Get to know MySQL database
Simple and beautiful weather code
Chapter 6: UART echo case of PS bare metal and FreeRTOS case development
JMeter uses JDBC to perform database pressure test
Tencent cloud CIF engineering effectiveness summit was successfully opened, and coding released a series of new products
Grpc: how to implement the restful API for file uploading?
2022-2028 global marine wet exhaust hose industry research and trend analysis report
What is elastic scaling in cloud computing? What are the main applications of elastic scaling in cloud computing?
2022-2028 global aircraft audio control panel system industry research and trend analysis report
What technology does cloud computing elasticity scale? What are the advantages of elastic scaling in cloud computing?
Under what circumstances do you need a fortress machine? What are the functions of a fortress machine
What is the role of the distributed configuration center? What are the advantages of a distributed configuration center?
[51nod] 2653 section XOR