diff --git a/app/api/controller/XunFeiController.php b/app/api/controller/XunFeiController.php index 6ce61fbdf..1b414aaa4 100644 --- a/app/api/controller/XunFeiController.php +++ b/app/api/controller/XunFeiController.php @@ -15,6 +15,7 @@ namespace app\api\controller; use IFlytek\Xfyun\Speech\ChatClient; +use IFlytek\Xfyun\Speech\IatClient; use WebSocket\Client; /** @@ -120,163 +121,21 @@ class XunFeiController extends BaseApiController public function iat() { + header('X-Accel-Buffering: no'); + $parmas=$this->request->param('content'); + $appid = "fa185cd6"; $apiSecret = "1474397d85f34828194622aab80f1e51"; $apiKey = "ZjQOYjhjMmE2NmMzYzhiMjU30GE1NjJl"; - $hostUrl = "ws://iat-api.xfyun.cn/v2/iat"; + $hostUrl = "wss://iat-api.xfyun.cn/v2/iat"; - // 请填写您的音频文件路径 - // $file = storage_path() . '/app/public/audio/1675996119143.mp3'; - - $st = microtime(true); - - $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); + // $iat=new IatClient($appid,$apiKey,$apiSecret); + $iat=new IatClient($this->app_id,$this->api_key,$this->api_secret); + halt($iat->assembleAuthUrl($hostUrl)); + $client = new Client($iat->assembleAuthUrl($hostUrl)); 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); - } } diff --git a/extend/IFlytek/Xfyun/Speech/ChatClient.php b/extend/IFlytek/Xfyun/Speech/ChatClient.php index acb1a1a02..0777b247b 100644 --- a/extend/IFlytek/Xfyun/Speech/ChatClient.php +++ b/extend/IFlytek/Xfyun/Speech/ChatClient.php @@ -61,14 +61,12 @@ class ChatClient $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); diff --git a/extend/IFlytek/Xfyun/Speech/IatClient.php b/extend/IFlytek/Xfyun/Speech/IatClient.php new file mode 100644 index 000000000..f8e2eae29 --- /dev/null +++ b/extend/IFlytek/Xfyun/Speech/IatClient.php @@ -0,0 +1,86 @@ +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; + } + +} diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index 210ff4cb3..9b943b17d 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -3,7 +3,7 @@ 'name' => 'topthink/think', 'pretty_version' => 'dev-master', 'version' => 'dev-master', - 'reference' => 'a05bdfc5cb5ebc4e7fddfde0185716a16d4898ca', + 'reference' => '21a9cb77e8da54d360c4d1c859fce545e4c365a4', 'type' => 'project', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), @@ -658,7 +658,7 @@ 'topthink/think' => array( 'pretty_version' => 'dev-master', 'version' => 'dev-master', - 'reference' => 'a05bdfc5cb5ebc4e7fddfde0185716a16d4898ca', + 'reference' => '21a9cb77e8da54d360c4d1c859fce545e4c365a4', 'type' => 'project', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), diff --git a/vendor/services.php b/vendor/services.php index fe2e2f3ca..439648f58 100644 --- a/vendor/services.php +++ b/vendor/services.php @@ -1,6 +1,6 @@ - 'think\\app\\Service', 1 => 'think\\queue\\Service',