更新获取签名
This commit is contained in:
parent
21a9cb77e8
commit
4778c87763
@ -15,6 +15,7 @@
|
|||||||
namespace app\api\controller;
|
namespace app\api\controller;
|
||||||
|
|
||||||
use IFlytek\Xfyun\Speech\ChatClient;
|
use IFlytek\Xfyun\Speech\ChatClient;
|
||||||
|
use IFlytek\Xfyun\Speech\IatClient;
|
||||||
use WebSocket\Client;
|
use WebSocket\Client;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -120,163 +121,21 @@ class XunFeiController extends BaseApiController
|
|||||||
|
|
||||||
public function iat()
|
public function iat()
|
||||||
{
|
{
|
||||||
|
header('X-Accel-Buffering: no');
|
||||||
|
$parmas=$this->request->param('content');
|
||||||
|
|
||||||
$appid = "fa185cd6";
|
$appid = "fa185cd6";
|
||||||
$apiSecret = "1474397d85f34828194622aab80f1e51";
|
$apiSecret = "1474397d85f34828194622aab80f1e51";
|
||||||
$apiKey = "ZjQOYjhjMmE2NmMzYzhiMjU30GE1NjJl";
|
$apiKey = "ZjQOYjhjMmE2NmMzYzhiMjU30GE1NjJl";
|
||||||
|
|
||||||
$hostUrl = "ws://iat-api.xfyun.cn/v2/iat";
|
$hostUrl = "wss://iat-api.xfyun.cn/v2/iat";
|
||||||
|
|
||||||
// 请填写您的音频文件路径
|
// $iat=new IatClient($appid,$apiKey,$apiSecret);
|
||||||
// $file = storage_path() . '/app/public/audio/1675996119143.mp3';
|
$iat=new IatClient($this->app_id,$this->api_key,$this->api_secret);
|
||||||
|
halt($iat->assembleAuthUrl($hostUrl));
|
||||||
$st = microtime(true);
|
$client = new Client($iat->assembleAuthUrl($hostUrl));
|
||||||
|
|
||||||
$authUrl = $this->assembleAuthUrl($hostUrl, $apiKey, $apiSecret);
|
|
||||||
|
|
||||||
$client = new Client($authUrl);
|
|
||||||
|
|
||||||
//打开音频文件
|
|
||||||
// $audioFile = fopen($file, 'rb');
|
|
||||||
// if ($audioFile === false) {
|
|
||||||
// throw new \Exception('open file error');
|
|
||||||
// }
|
|
||||||
|
|
||||||
$frameSize = 1280; //每一帧的音频大小
|
|
||||||
$intervel = 40 * 1000; //发送音频间隔
|
|
||||||
$status = 0;
|
|
||||||
|
|
||||||
|
|
||||||
// 连接到 WebSocket 服务器
|
|
||||||
if ($client) {
|
|
||||||
/*
|
|
||||||
//发送数据
|
|
||||||
while (true) {
|
|
||||||
$len = fread($audioFile, $frameSize);
|
|
||||||
if ($len === false) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if ($len === '') { //文件读取完了,改变status = STATUS_LAST_FRAME
|
|
||||||
$status = 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ($status) {
|
|
||||||
case 0: //发送第一帧音频,带business 参数
|
|
||||||
$frameData = array(
|
|
||||||
'common' => array(
|
|
||||||
'app_id' => $appid //appid 必须带上,只需第一帧发送
|
|
||||||
),
|
|
||||||
'business' => array( //business 参数,只需一帧发送
|
|
||||||
'language' => 'en_us',
|
|
||||||
'domain' => 'iat',
|
|
||||||
// 'accent' => 'mandarin'
|
|
||||||
),
|
|
||||||
'data' => array(
|
|
||||||
'status' => 0,
|
|
||||||
'format' => 'audio/L16;rate=16000',
|
|
||||||
'audio' => base64_encode($len),
|
|
||||||
'encoding' => 'lame'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$conn->send(json_encode($frameData));
|
|
||||||
$status = 1;
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
$frameData = array(
|
|
||||||
'data' => array(
|
|
||||||
'status' => 1,
|
|
||||||
'format' => 'audio/L16;rate=16000',
|
|
||||||
'audio' => base64_encode($len),
|
|
||||||
'encoding' => 'raw'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$conn->send(json_encode($frameData));
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
$frameData = array(
|
|
||||||
'data' => array(
|
|
||||||
'status' => 2,
|
|
||||||
'format' => 'audio/L16;rate=16000',
|
|
||||||
'audio' => base64_encode($len),
|
|
||||||
'encoding' => 'raw'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$conn->send(json_encode($frameData));
|
|
||||||
break 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
//模拟音频采样间隔
|
|
||||||
usleep($intervel);
|
|
||||||
}
|
|
||||||
|
|
||||||
//获取返回的数据
|
|
||||||
while (true) {
|
|
||||||
$msg = $client->receive();
|
|
||||||
if ($msg === null) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
$resp = json_decode($msg, true);
|
|
||||||
|
|
||||||
$code = $resp['code'];
|
|
||||||
$message = $resp['message'];
|
|
||||||
$data = $resp['data'];
|
|
||||||
$result = $data['result'];
|
|
||||||
$status = $data['status'];
|
|
||||||
$sid = $resp['sid'];
|
|
||||||
|
|
||||||
if ($code != 0) {
|
|
||||||
echo sprintf('%d %s %f', $code, $message, microtime(true) - $st);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
var_dump($resp, $result);
|
|
||||||
|
|
||||||
|
|
||||||
if ($status === 2) {
|
|
||||||
echo sprintf('%d %s %f', $code, $message, microtime(true) - $st);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
echo "连接到 WebSocket 服务器";
|
|
||||||
} else {
|
|
||||||
echo "无法连接到 WebSocket 服务器";
|
|
||||||
}
|
|
||||||
|
|
||||||
// fclose($audioFile);
|
|
||||||
usleep(1); //等待数据接收完成
|
usleep(1); //等待数据接收完成
|
||||||
}
|
}
|
||||||
|
|
||||||
function assembleAuthUrl($hostUrl, $apiKey, $apiSecret)
|
|
||||||
{
|
|
||||||
$ul = parse_url($hostUrl);
|
|
||||||
$date = gmdate('D, d M Y H:i:s T');
|
|
||||||
$signString = array(
|
|
||||||
'host: ' . $ul['host'],
|
|
||||||
'date: ' . $date,
|
|
||||||
'GET ' . $ul['path'] . ' HTTP/1.1'
|
|
||||||
);
|
|
||||||
$sgin = implode("\n", $signString);
|
|
||||||
$sha = $this->HmacWithShaTobase64("hmac-sha256", $sgin, $apiSecret);
|
|
||||||
|
|
||||||
$authUrl = sprintf('api_key="%s", algorithm="%s", headers="%s", signature="%s"',
|
|
||||||
$apiKey, "hmac-sha256", "host date request-line", $sha);
|
|
||||||
$authorization = base64_encode($authUrl);
|
|
||||||
$v = array(
|
|
||||||
'host' => $ul['host'],
|
|
||||||
'date' => $date,
|
|
||||||
'authorization' => $authorization
|
|
||||||
);
|
|
||||||
$query = http_build_query($v);
|
|
||||||
$callurl = $hostUrl . '?' . $query;
|
|
||||||
return $callurl;
|
|
||||||
}
|
|
||||||
|
|
||||||
function HmacWithShaTobase64($algorithm, $data, $key)
|
|
||||||
{
|
|
||||||
$encodeData = hash_hmac('sha256', $data, $key, true);
|
|
||||||
return base64_encode($encodeData);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -61,14 +61,12 @@ class ChatClient
|
|||||||
$timestamp = time();
|
$timestamp = time();
|
||||||
$rfc1123_format = gmdate("D, d M Y H:i:s \G\M\T", $timestamp);
|
$rfc1123_format = gmdate("D, d M Y H:i:s \G\M\T", $timestamp);
|
||||||
// $rfc1123_format = "Mon, 31 Jul 2023 08:24:03 GMT";
|
// $rfc1123_format = "Mon, 31 Jul 2023 08:24:03 GMT";
|
||||||
|
|
||||||
// 参与签名的字段 host, date, request-line
|
// 参与签名的字段 host, date, request-line
|
||||||
$signString = array("host: " . $ul["host"], "date: " . $rfc1123_format, $method . " " . $ul["path"] . " HTTP/1.1");
|
$signString = array("host: " . $ul["host"], "date: " . $rfc1123_format, $method . " " . $ul["path"] . " HTTP/1.1");
|
||||||
// 对签名字符串进行排序,确保顺序一致
|
// 对签名字符串进行排序,确保顺序一致
|
||||||
// ksort($signString);
|
// ksort($signString);
|
||||||
// 将签名字符串拼接成一个字符串
|
// 将签名字符串拼接成一个字符串
|
||||||
$sgin = implode("\n", $signString);
|
$sgin = implode("\n", $signString);
|
||||||
|
|
||||||
// 对签名字符串进行HMAC-SHA256加密,得到签名结果
|
// 对签名字符串进行HMAC-SHA256加密,得到签名结果
|
||||||
$sha = hash_hmac('sha256', $sgin, $apiSecret,true);
|
$sha = hash_hmac('sha256', $sgin, $apiSecret,true);
|
||||||
|
|
||||||
|
86
extend/IFlytek/Xfyun/Speech/IatClient.php
Normal file
86
extend/IFlytek/Xfyun/Speech/IatClient.php
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2022 iFLYTEK Corporation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the 'License');
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an 'AS IS' BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace IFlytek\Xfyun\Speech;
|
||||||
|
|
||||||
|
use IFlytek\Xfyun\Core\Traits\SignTrait;
|
||||||
|
use IFlytek\Xfyun\Core\HttpClient;
|
||||||
|
/**
|
||||||
|
* 讯飞Ai户端
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class IatClient
|
||||||
|
{
|
||||||
|
use SignTrait;
|
||||||
|
|
||||||
|
protected $appId;
|
||||||
|
protected $apiKey;
|
||||||
|
protected $apiSecret;
|
||||||
|
protected $uid;
|
||||||
|
protected $resId;
|
||||||
|
protected $requestConfig;
|
||||||
|
protected $client;
|
||||||
|
|
||||||
|
public function __construct($appId, $apiKey, $apiSecret, $uid = null, $resId = null, $requestConfig = [])
|
||||||
|
{
|
||||||
|
$this->appId = $appId;
|
||||||
|
$this->apiKey = $apiKey;
|
||||||
|
$this->apiSecret = $apiSecret;
|
||||||
|
$this->uid = $uid;
|
||||||
|
$this->resId = $resId;
|
||||||
|
$this->client = new HttpClient([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function assembleAuthUrl($addr,$method='GET') {
|
||||||
|
$apiKey=$this->apiKey;
|
||||||
|
$apiSecret=$this->apiSecret;
|
||||||
|
if ($apiKey == "" && $apiSecret == "") { // 不鉴权
|
||||||
|
return $addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
$ul = parse_url($addr); // 解析地址
|
||||||
|
if ($ul === false) { // 地址不对,也不鉴权
|
||||||
|
return $addr;
|
||||||
|
}
|
||||||
|
// // $date = date(DATE_RFC1123); // 获取当前时间并格式化为RFC1123格式的字符串
|
||||||
|
$timestamp = time();
|
||||||
|
$rfc1123_format = gmdate("D, d M Y H:i:s \G\M\T", $timestamp);
|
||||||
|
// $rfc1123_format = "Mon, 31 Jul 2023 08:24:03 GMT";
|
||||||
|
// 参与签名的字段 host, date, request-line
|
||||||
|
$signString = array("host: " . $ul["host"], "date: " . $rfc1123_format, $method . " " . $ul["path"] . " HTTP/1.1");
|
||||||
|
// 对签名字符串进行排序,确保顺序一致
|
||||||
|
// ksort($signString);
|
||||||
|
// 将签名字符串拼接成一个字符串
|
||||||
|
$sgin = implode("\n", $signString);
|
||||||
|
// 对签名字符串进行HMAC-SHA256加密,得到签名结果
|
||||||
|
$sha = hash_hmac('sha256', $sgin, $apiSecret,true);
|
||||||
|
|
||||||
|
$signature_sha_base64 = base64_encode($sha);
|
||||||
|
|
||||||
|
// 将API密钥、算法、头部信息和签名结果拼接成一个授权URL
|
||||||
|
$authUrl = "api_key=\"$apiKey\",algorithm=\"hmac-sha256\",headers=\"host date request-line\",signature=\"$signature_sha_base64\"";
|
||||||
|
// 对授权URL进行Base64编码,并添加到原始地址后面作为查询参数
|
||||||
|
$authAddr = $addr . '?' . http_build_query(array(
|
||||||
|
'host' => $ul['host'],
|
||||||
|
'date' => $rfc1123_format,
|
||||||
|
'authorization' => base64_encode($authUrl),
|
||||||
|
));
|
||||||
|
return $authAddr;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
4
vendor/composer/installed.php
vendored
4
vendor/composer/installed.php
vendored
@ -3,7 +3,7 @@
|
|||||||
'name' => 'topthink/think',
|
'name' => 'topthink/think',
|
||||||
'pretty_version' => 'dev-master',
|
'pretty_version' => 'dev-master',
|
||||||
'version' => 'dev-master',
|
'version' => 'dev-master',
|
||||||
'reference' => 'a05bdfc5cb5ebc4e7fddfde0185716a16d4898ca',
|
'reference' => '21a9cb77e8da54d360c4d1c859fce545e4c365a4',
|
||||||
'type' => 'project',
|
'type' => 'project',
|
||||||
'install_path' => __DIR__ . '/../../',
|
'install_path' => __DIR__ . '/../../',
|
||||||
'aliases' => array(),
|
'aliases' => array(),
|
||||||
@ -658,7 +658,7 @@
|
|||||||
'topthink/think' => array(
|
'topthink/think' => array(
|
||||||
'pretty_version' => 'dev-master',
|
'pretty_version' => 'dev-master',
|
||||||
'version' => 'dev-master',
|
'version' => 'dev-master',
|
||||||
'reference' => 'a05bdfc5cb5ebc4e7fddfde0185716a16d4898ca',
|
'reference' => '21a9cb77e8da54d360c4d1c859fce545e4c365a4',
|
||||||
'type' => 'project',
|
'type' => 'project',
|
||||||
'install_path' => __DIR__ . '/../../',
|
'install_path' => __DIR__ . '/../../',
|
||||||
'aliases' => array(),
|
'aliases' => array(),
|
||||||
|
2
vendor/services.php
vendored
2
vendor/services.php
vendored
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
// This file is automatically generated at:2023-10-09 11:29:44
|
// This file is automatically generated at:2023-10-09 22:37:30
|
||||||
declare (strict_types = 1);
|
declare (strict_types = 1);
|
||||||
return array (
|
return array (
|
||||||
0 => 'think\\app\\Service',
|
0 => 'think\\app\\Service',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user