讯飞
This commit is contained in:
parent
c4b518e85e
commit
b905b58053
45
app/api/controller/XunFeiController.php
Normal file
45
app/api/controller/XunFeiController.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||||
|
// | 开源版本可自由商用,可去除界面版权logo
|
||||||
|
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||||
|
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||||
|
// | 访问官网:https://www.likeadmin.cn
|
||||||
|
// | likeadmin团队 版权所有 拥有最终解释权
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | author: likeadminTeam
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace app\api\controller;
|
||||||
|
|
||||||
|
use IFlytek\Xfyun\Speech\TcClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 讯飞
|
||||||
|
* Class WechatController
|
||||||
|
*/
|
||||||
|
class XunFeiController extends BaseApiController
|
||||||
|
{
|
||||||
|
public array $notNeedLogin = ['ceshi'];
|
||||||
|
|
||||||
|
private $app_id='2eda6c2e';
|
||||||
|
|
||||||
|
private $api_key='MDEyMzE5YTc5YmQ5NjMwOTU1MWY4N2Y2';
|
||||||
|
|
||||||
|
private $api_secret='12ec1f9d113932575fc4b114a2f60ffd';
|
||||||
|
|
||||||
|
public function ceshi()
|
||||||
|
{
|
||||||
|
// 这里的$app_id、$api_key、$api_secret是在开放平台控制台获得
|
||||||
|
$client = new TcClient($this->app_id, $this->api_key, $this->api_secret);
|
||||||
|
|
||||||
|
// 文本纠错请求,返回格式为json字符串
|
||||||
|
$content = $client->request('历史上有很多注明的人物,其中唐太宗李世民就是一位。')->getBody()->getContents();
|
||||||
|
halt($content);
|
||||||
|
// 黑白名单上传请求,成功返回true,失败返回false(失败请检查uid、res_id是否设置)
|
||||||
|
// $client = new TcClient($this->app_id, $this->api_key, $api_secret, $uid, $res_id);
|
||||||
|
// $client->listUpload($white_list, $black_list);
|
||||||
|
}
|
||||||
|
}
|
@ -36,7 +36,8 @@
|
|||||||
"w7corp/easywechat": "^6.8",
|
"w7corp/easywechat": "^6.8",
|
||||||
"yunwuxin/think-cron": "^3.0",
|
"yunwuxin/think-cron": "^3.0",
|
||||||
"topthink/think-queue": "^3.0",
|
"topthink/think-queue": "^3.0",
|
||||||
"firebase/php-jwt": "^6.8"
|
"firebase/php-jwt": "^6.8",
|
||||||
|
"guzzlehttp/guzzle": "^7.8"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"symfony/var-dumper": "^4.2",
|
"symfony/var-dumper": "^4.2",
|
||||||
|
46
composer.lock
generated
46
composer.lock
generated
@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "fdd9d31f9958a1263e4623ab634b9cc9",
|
"content-hash": "24f0a1ed76959703117054b6cb06afd8",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "adbario/php-dot-notation",
|
"name": "adbario/php-dot-notation",
|
||||||
@ -542,28 +542,17 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "guzzlehttp/guzzle",
|
"name": "guzzlehttp/guzzle",
|
||||||
"version": "7.7.0",
|
"version": "7.8.0",
|
||||||
"source": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "https://github.com/guzzle/guzzle.git",
|
|
||||||
"reference": "fb7566caccf22d74d1ab270de3551f72a58399f5"
|
|
||||||
},
|
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/guzzle/guzzle/zipball/fb7566caccf22d74d1ab270de3551f72a58399f5",
|
"url": "https://mirrors.tencent.com/repository/composer/guzzlehttp/guzzle/7.8.0/guzzlehttp-guzzle-7.8.0.zip",
|
||||||
"reference": "fb7566caccf22d74d1ab270de3551f72a58399f5",
|
"reference": "1110f66a6530a40fe7aea0378fe608ee2b2248f9",
|
||||||
"shasum": "",
|
"shasum": ""
|
||||||
"mirrors": [
|
|
||||||
{
|
|
||||||
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
|
|
||||||
"preferred": true
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"guzzlehttp/promises": "^1.5.3 || ^2.0",
|
"guzzlehttp/promises": "^1.5.3 || ^2.0.1",
|
||||||
"guzzlehttp/psr7": "^1.9.1 || ^2.4.5",
|
"guzzlehttp/psr7": "^1.9.1 || ^2.5.1",
|
||||||
"php": "^7.2.5 || ^8.0",
|
"php": "^7.2.5 || ^8.0",
|
||||||
"psr/http-client": "^1.0",
|
"psr/http-client": "^1.0",
|
||||||
"symfony/deprecation-contracts": "^2.2 || ^3.0"
|
"symfony/deprecation-contracts": "^2.2 || ^3.0"
|
||||||
@ -599,7 +588,6 @@
|
|||||||
"GuzzleHttp\\": "src/"
|
"GuzzleHttp\\": "src/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"notification-url": "https://packagist.org/downloads/",
|
|
||||||
"license": [
|
"license": [
|
||||||
"MIT"
|
"MIT"
|
||||||
],
|
],
|
||||||
@ -652,25 +640,7 @@
|
|||||||
"rest",
|
"rest",
|
||||||
"web service"
|
"web service"
|
||||||
],
|
],
|
||||||
"support": {
|
"time": "2023-08-27T10:20:53+00:00"
|
||||||
"issues": "https://github.com/guzzle/guzzle/issues",
|
|
||||||
"source": "https://github.com/guzzle/guzzle/tree/7.7.0"
|
|
||||||
},
|
|
||||||
"funding": [
|
|
||||||
{
|
|
||||||
"url": "https://github.com/GrahamCampbell",
|
|
||||||
"type": "github"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"url": "https://github.com/Nyholm",
|
|
||||||
"type": "github"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"url": "https://tidelift.com/funding/github/packagist/guzzlehttp/guzzle",
|
|
||||||
"type": "tidelift"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"time": "2023-05-21T14:04:53+00:00"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "guzzlehttp/guzzle-services",
|
"name": "guzzlehttp/guzzle-services",
|
||||||
|
41
extend/IFlytek/Xfyun/Core/Config/ConfigInterface.php
Normal file
41
extend/IFlytek/Xfyun/Core/Config/ConfigInterface.php
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Core\Config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置类接口
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
interface ConfigInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 返回配置内容的json形式
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function toJson();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回配置内容的数组形式
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toArray();
|
||||||
|
}
|
53
extend/IFlytek/Xfyun/Core/Handler/Guzzle7HttpHandler.php
Normal file
53
extend/IFlytek/Xfyun/Core/Handler/Guzzle7HttpHandler.php
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Core\Handler;
|
||||||
|
use GuzzleHttp\ClientInterface;
|
||||||
|
use Psr\Http\Message\RequestInterface;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
/**
|
||||||
|
* GuzzleHttp-7处理类
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class Guzzle7HttpHandler{
|
||||||
|
/**
|
||||||
|
* @var ClientInterface
|
||||||
|
*/
|
||||||
|
private $client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ClientInterface $client
|
||||||
|
*/
|
||||||
|
public function __construct(ClientInterface $client)
|
||||||
|
{
|
||||||
|
$this->client = $client;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 接收Psr-7的request对象和请求参数,返回Psr-7的response对象
|
||||||
|
*
|
||||||
|
* @param RequestInterface $request
|
||||||
|
* @param array $options
|
||||||
|
* @return ResponseInterface
|
||||||
|
*/
|
||||||
|
public function __invoke(RequestInterface $request, array $options = [])
|
||||||
|
{
|
||||||
|
return $this->client->send($request, $options);
|
||||||
|
}
|
||||||
|
}
|
45
extend/IFlytek/Xfyun/Core/Handler/HttpHandlerFactory.php
Normal file
45
extend/IFlytek/Xfyun/Core/Handler/HttpHandlerFactory.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Core\Handler;
|
||||||
|
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
use GuzzleHttp\ClientInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HttpHandler工厂类
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class HttpHandlerFactory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 根据安装的GuzzleHttp版本获取默认的处理类
|
||||||
|
*
|
||||||
|
* @param ClientInterface $client
|
||||||
|
* @return Guzzle6HttpHandler|Guzzle7HttpHandler
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public static function build(ClientInterface $client = null)
|
||||||
|
{
|
||||||
|
$client = $client ?: new Client();
|
||||||
|
|
||||||
|
return new Guzzle7HttpHandler($client);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
126
extend/IFlytek/Xfyun/Core/Handler/WsHandler.php
Normal file
126
extend/IFlytek/Xfyun/Core/Handler/WsHandler.php
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Core\Handler;
|
||||||
|
|
||||||
|
use IFlytek\Xfyun\Core\Traits\JsonTrait;
|
||||||
|
use WebSocket\Client;
|
||||||
|
use WebSocket\Exception;
|
||||||
|
use GuzzleHttp\Psr7\Response;
|
||||||
|
use Psr\Log\{LoggerAwareInterface, LoggerInterface, NullLogger};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WebSocket处理类
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class WsHandler implements LoggerAwareInterface
|
||||||
|
{
|
||||||
|
use JsonTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var WebSocket\Client ws client
|
||||||
|
*/
|
||||||
|
private $client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 发送的字符串
|
||||||
|
*/
|
||||||
|
private $input;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var LoggerInterface or null 日志处理
|
||||||
|
*/
|
||||||
|
private $logger;
|
||||||
|
|
||||||
|
public function __construct($uri, $input, $timeout = 300, $logger = null)
|
||||||
|
{
|
||||||
|
$this->client = new Client($uri);
|
||||||
|
$this->client->setTimeout($timeout);
|
||||||
|
$this->input = $input;
|
||||||
|
$this->logger = $logger ?: new NullLogger();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送并等待获取返回
|
||||||
|
*
|
||||||
|
* 这是一个同步阻塞的过程,调用将持续到结果返回或者出现超时
|
||||||
|
*/
|
||||||
|
public function sendAndReceive()
|
||||||
|
{
|
||||||
|
$result = '';
|
||||||
|
try {
|
||||||
|
$this->logger->info("Start to send data, input: {$this->input}");
|
||||||
|
$this->client->send($this->input);
|
||||||
|
$printSid = true;
|
||||||
|
while (true) {
|
||||||
|
$message = $this->jsonDecode($this->client->receive());
|
||||||
|
if ($message->code !== 0) {
|
||||||
|
throw new \Exception(json_encode($message));
|
||||||
|
}
|
||||||
|
if ($printSid) {
|
||||||
|
$this->logger->info("Receiving data, sid-[{$message->sid}]");
|
||||||
|
$printSid = false;
|
||||||
|
}
|
||||||
|
switch ($message->data->status) {
|
||||||
|
case 1:
|
||||||
|
$result .= base64_decode($message->data->audio);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
$result .= base64_decode($message->data->audio);
|
||||||
|
break 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->logger->info("Receive data successfully, total length: " . strlen($result));
|
||||||
|
return new Response(200, [], $result);
|
||||||
|
} catch (Exception $ex) {
|
||||||
|
throw $ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function send($message = null)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if (empty($message)) {
|
||||||
|
if (!empty($this->input)) {
|
||||||
|
$message = $this->input;
|
||||||
|
} else {
|
||||||
|
throw new Exception();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this->client->send($message);
|
||||||
|
} catch (Exception $ex) {
|
||||||
|
throw $ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function receive()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return $this->client->receive();
|
||||||
|
} catch (Exception $ex) {
|
||||||
|
throw $ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setLogger(LoggerInterface $logger = null)
|
||||||
|
{
|
||||||
|
$this->logger = $logger ?: new NullLogger();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
163
extend/IFlytek/Xfyun/Core/HttpClient.php
Normal file
163
extend/IFlytek/Xfyun/Core/HttpClient.php
Normal file
@ -0,0 +1,163 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Core;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Http客户端
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use GuzzleHttp\Psr7\Response;
|
||||||
|
use IFlytek\Xfyun\Core\Handler\Guzzle7HttpHandler;
|
||||||
|
use IFlytek\Xfyun\Core\Traits\SignTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Traits\DecideRetryTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Handler\HttpHandlerFactory;
|
||||||
|
use Psr\Http\Message\RequestInterface;
|
||||||
|
use GuzzleHttp\Psr7\Utils;
|
||||||
|
|
||||||
|
class HttpClient
|
||||||
|
{
|
||||||
|
use SignTrait;
|
||||||
|
use DecideRetryTrait;
|
||||||
|
|
||||||
|
const MAX_DELAY_MICROSECONDS = 60000000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Guzzle7HttpHandler
|
||||||
|
*/
|
||||||
|
private $httpHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array 要添加的头部信息
|
||||||
|
*/
|
||||||
|
private $httpHeaders;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int 超时时间
|
||||||
|
*/
|
||||||
|
private $requestTimeout;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int 重试次数
|
||||||
|
*/
|
||||||
|
private $retries;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int 重试次数
|
||||||
|
*/
|
||||||
|
private $decideRetryFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int 重试次数
|
||||||
|
*/
|
||||||
|
private $delayFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int 重试次数
|
||||||
|
*/
|
||||||
|
private $calcDelayFunction;
|
||||||
|
|
||||||
|
public function __construct($config)
|
||||||
|
{
|
||||||
|
$config += [
|
||||||
|
'httpHandler' => null,
|
||||||
|
'httpHeaders' => [],
|
||||||
|
'requestTimeout' => 3000,
|
||||||
|
'retries' => 3,
|
||||||
|
'decideRetryFunction' => null,
|
||||||
|
'delayFunction' => null,
|
||||||
|
'calcDelayFunction' => null
|
||||||
|
];
|
||||||
|
$this->httpHandler = $config['httpHandler'] ?: HttpHandlerFactory::build();
|
||||||
|
$this->httpHeaders = $config['httpHeaders'];
|
||||||
|
$this->retries = $config['retries'];
|
||||||
|
$this->decideRetryFunction = $config['decideRetryFunction'] ?: $this->getDecideRetryFunction();
|
||||||
|
$this->calcDelayFunction = $config['calcDelayFunction'] ?: [$this, 'calculateDelay'];
|
||||||
|
$this->delayFunction = $config['delayFunction'] ?: static function ($delay) {
|
||||||
|
usleep($delay);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送请求,并接受返回
|
||||||
|
*
|
||||||
|
* @param RequestInterface $request
|
||||||
|
* @param array $options 请求的配置参数
|
||||||
|
* @return Response
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function sendAndReceive(RequestInterface $request, array $options = [])
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$delayFunction = $this->delayFunction;
|
||||||
|
$calcDelayFunction = $this->calcDelayFunction;
|
||||||
|
$retryAttempt = 0;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
return call_user_func_array($this->httpHandler, [$this->applyHeaders($request), $options]);
|
||||||
|
} catch (Exception $exception) {
|
||||||
|
if ($this->decideRetryFunction) {
|
||||||
|
if (!call_user_func($this->decideRetryFunction, $exception)) {
|
||||||
|
throw $exception;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($retryAttempt >= $this->retries) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$delayFunction($calcDelayFunction($retryAttempt));
|
||||||
|
$retryAttempt++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw $exception;
|
||||||
|
} catch (Exception $ex) {
|
||||||
|
throw $ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将头部信息添加到请求对象中
|
||||||
|
*
|
||||||
|
* @param $request 请求对象
|
||||||
|
* @return RequestInterface
|
||||||
|
*/
|
||||||
|
private function applyHeaders($request)
|
||||||
|
{
|
||||||
|
return Utils::modifyRequest($request, ['set_headers' => $this->httpHeaders]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据重试次数计算下次重试延迟时间
|
||||||
|
*
|
||||||
|
* @param int $attempt 重试次数
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public static function calculateDelay($attempt)
|
||||||
|
{
|
||||||
|
return min(
|
||||||
|
mt_rand(0, 1000000) + (pow(2, $attempt) * 1000000),
|
||||||
|
self::MAX_DELAY_MICROSECONDS
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
45
extend/IFlytek/Xfyun/Core/Traits/ArrayTrait.php
Normal file
45
extend/IFlytek/Xfyun/Core/Traits/ArrayTrait.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Core\Traits;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数组处理
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
trait ArrayTrait
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 递归移除数组中的null项
|
||||||
|
*
|
||||||
|
* @param array $array
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private static function removeNull($array)
|
||||||
|
{
|
||||||
|
foreach ($array as $key => $item) {
|
||||||
|
if (is_array($item)) {
|
||||||
|
$array[$key] = self::removeNull($item);
|
||||||
|
} else if (is_null($item)) {
|
||||||
|
unset($array[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
}
|
96
extend/IFlytek/Xfyun/Core/Traits/DecideRetryTrait.php
Normal file
96
extend/IFlytek/Xfyun/Core/Traits/DecideRetryTrait.php
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Core\Traits;
|
||||||
|
|
||||||
|
use GuzzleHttp\Exception\RequestException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提供一个方法,决定是否进行重试
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
trait DecideRetryTrait
|
||||||
|
{
|
||||||
|
use JsonTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $httpRetryCodes = [
|
||||||
|
500,
|
||||||
|
502,
|
||||||
|
503
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $httpRetryMessages = [
|
||||||
|
'retry later'
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回一个callable变量,作用是决定是否重试
|
||||||
|
*
|
||||||
|
* @param bool $shouldRetryMessages 是否要根据message决定重试与否
|
||||||
|
* @return callable
|
||||||
|
*/
|
||||||
|
private function getDecideRetryFunction($shouldRetryMessages = true)
|
||||||
|
{
|
||||||
|
$httpRetryCodes = $this->httpRetryCodes;
|
||||||
|
$httpRetryMessages = $this->httpRetryMessages;
|
||||||
|
|
||||||
|
return function (\Exception $ex) use ($httpRetryCodes, $httpRetryMessages, $shouldRetryMessages) {
|
||||||
|
$statusCode = $ex->getCode();
|
||||||
|
|
||||||
|
if (in_array($statusCode, $httpRetryCodes)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$shouldRetryMessages) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$message = ($ex instanceof RequestException && $ex->hasResponse())
|
||||||
|
? (string) $ex->getResponse()->getBody()
|
||||||
|
: $ex->getMessage();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$message = $this->jsonDecode(
|
||||||
|
$message,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
} catch (\InvalidArgumentException $ex) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($message['errors'])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($message['errors'] as $error) {
|
||||||
|
if (in_array($error['reason'], $httpRetryMessages)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
68
extend/IFlytek/Xfyun/Core/Traits/JsonTrait.php
Normal file
68
extend/IFlytek/Xfyun/Core/Traits/JsonTrait.php
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Core\Traits;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提供对原生json_encode和json_decode的封装,以便在发生错误时抛出一个异常
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
trait JsonTrait
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param string $json 待解码的字符串
|
||||||
|
* @param bool $assoc 是否返回数组
|
||||||
|
* @param int $depth 递归深度
|
||||||
|
* @param int $options json_decode的配置
|
||||||
|
* @return mixed
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
private static function jsonDecode($json, $assoc = false, $options = 0, $depth = 512)
|
||||||
|
{
|
||||||
|
$data = json_decode($json, $assoc, $depth, $options);
|
||||||
|
|
||||||
|
if (JSON_ERROR_NONE !== json_last_error()) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
|
'json_decode error: ' . json_last_error_msg()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value 待编码的变量
|
||||||
|
* @param int $options json_decode的配置
|
||||||
|
* @param int $depth 递归深度
|
||||||
|
* @return string
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
private static function jsonEncode($value, $options = 0, $depth = 512)
|
||||||
|
{
|
||||||
|
$json = json_encode($value, $options, $depth);
|
||||||
|
|
||||||
|
if (JSON_ERROR_NONE !== json_last_error()) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
|
'json_encode error: ' . json_last_error_msg()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $json;
|
||||||
|
}
|
||||||
|
}
|
93
extend/IFlytek/Xfyun/Core/Traits/SignTrait.php
Normal file
93
extend/IFlytek/Xfyun/Core/Traits/SignTrait.php
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Core\Traits;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提供平台的签名方法
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
trait SignTrait
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据secret对uri进行签名,返回签名后的uri
|
||||||
|
*
|
||||||
|
* @param string $uri 待签名的uri
|
||||||
|
* @param array $secret 秘钥信息
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private static function signUriV1($uri, $secret)
|
||||||
|
{
|
||||||
|
$apiKey = $secret['apiKey'];
|
||||||
|
$apiSecret = $secret['apiSecret'];
|
||||||
|
$host = $secret['host'];
|
||||||
|
$request_line = $secret['requestLine'];
|
||||||
|
$date = empty($secret['date']) ? gmdate ('D, d M Y H:i:s \G\M\T', time()) : $secret['date'];
|
||||||
|
$signature_origin = "host: $host\ndate: $date\n$request_line";
|
||||||
|
$signature_sha = hash_hmac('sha256', $signature_origin, $apiSecret, true);
|
||||||
|
$signature = base64_encode($signature_sha);
|
||||||
|
|
||||||
|
$authrization = base64_encode("api_key=\"$apiKey\",algorithm=\"hmac-sha256\",headers=\"host date request-line\",signature=\"$signature\"");
|
||||||
|
$uri = $uri . '?' . http_build_query([
|
||||||
|
'host' => $host,
|
||||||
|
'date' => $date,
|
||||||
|
'authorization' => $authrization
|
||||||
|
]);
|
||||||
|
return $uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据所提供信息返回签名
|
||||||
|
*
|
||||||
|
* @param string $appId appid
|
||||||
|
* @param string $secretKey secretKey
|
||||||
|
* @param string $timestamp 时间戳,不传的话使用系统时间
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function signV1($appId, $secretKey, $timestamp = null)
|
||||||
|
{
|
||||||
|
$timestamp = $timestamp ?: time();
|
||||||
|
$baseString = $appId . $timestamp;
|
||||||
|
$signa_origin = hash_hmac('sha1', md5($baseString), $secretKey, true);
|
||||||
|
return base64_encode($signa_origin);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https调用的鉴权参数构造
|
||||||
|
*
|
||||||
|
* @param string $appId appId
|
||||||
|
* @param string $apiKey apiKey
|
||||||
|
* @param string $curTime curTime
|
||||||
|
* @param string $param param
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function signV2($appId, $apiKey, $param, $curTime = null)
|
||||||
|
{
|
||||||
|
if (empty($curTime)) {
|
||||||
|
$curTime = time();
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
'X-Appid' => $appId,
|
||||||
|
'X-CurTime' => $curTime,
|
||||||
|
'X-Param' => base64_encode($param),
|
||||||
|
'X-CheckSum' => md5($apiKey . $curTime . base64_encode($param))
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
70
extend/IFlytek/Xfyun/Core/WsClient.php
Normal file
70
extend/IFlytek/Xfyun/Core/WsClient.php
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Core;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WebSocket客户端
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class WsClient
|
||||||
|
{
|
||||||
|
/** @var WsHandler */
|
||||||
|
private $handler;
|
||||||
|
|
||||||
|
public function __construct($config)
|
||||||
|
{
|
||||||
|
$config += [
|
||||||
|
'handler' => null,
|
||||||
|
];
|
||||||
|
$this->handler = $config['handler'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送请求,并接受返回
|
||||||
|
*
|
||||||
|
* @return Response
|
||||||
|
*/
|
||||||
|
public function sendAndReceive()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return call_user_func_array([$this->handler, 'sendAndReceive'], []);
|
||||||
|
} catch (\Exception $ex) {
|
||||||
|
throw $ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function send($message = null)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return call_user_func_array([$this->handler, 'send'], [$message]);
|
||||||
|
} catch (\Exception $ex) {
|
||||||
|
throw $ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function receive()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return call_user_func_array([$this->handler, 'receive'], []);
|
||||||
|
} catch (\Exception $ex) {
|
||||||
|
throw $ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
110
extend/IFlytek/Xfyun/Speech/Config/IgrConfig.php
Normal file
110
extend/IFlytek/Xfyun/Speech/Config/IgrConfig.php
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Config;
|
||||||
|
|
||||||
|
use IFlytek\Xfyun\Core\Traits\ArrayTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Traits\JsonTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Config\ConfigInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 性别年龄识别配置参数类
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class IgrConfig implements ConfigInterface
|
||||||
|
{
|
||||||
|
use ArrayTrait;
|
||||||
|
use JsonTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 引擎类型
|
||||||
|
* 仅支持igr
|
||||||
|
*/
|
||||||
|
private $ent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 音频格式
|
||||||
|
* raw:原生音频数据pcm格式
|
||||||
|
* speex:speex格式(rate需设置为8000)
|
||||||
|
* speex-wb:宽频speex格式(rate需设置为16000)
|
||||||
|
* amr:amr格式(rate需设置为8000)
|
||||||
|
* amr-wb:宽频amr格式(rate需设置为16000)
|
||||||
|
* 默认raw
|
||||||
|
*/
|
||||||
|
private $aue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int 音频采样率
|
||||||
|
* 16000/8000
|
||||||
|
* 默认16000
|
||||||
|
*/
|
||||||
|
private $rate;
|
||||||
|
|
||||||
|
public function __construct($config)
|
||||||
|
{
|
||||||
|
$config += [
|
||||||
|
'aue' => 'raw',
|
||||||
|
'rate' => 16000
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->ent = 'igr';
|
||||||
|
$this->aue = $config['aue'];
|
||||||
|
$this->rate = $config['rate'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 去除null项后返回数组形式
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toArray()
|
||||||
|
{
|
||||||
|
return $this->removeNull([
|
||||||
|
'ent' => $this->ent,
|
||||||
|
'aue' => $this->aue,
|
||||||
|
'rate' => $this->rate
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回toArray的Json格式
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function toJson()
|
||||||
|
{
|
||||||
|
return $this->jsonEncode($this->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getAue()
|
||||||
|
{
|
||||||
|
return $this->aue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getRate()
|
||||||
|
{
|
||||||
|
return $this->rate;
|
||||||
|
}
|
||||||
|
}
|
265
extend/IFlytek/Xfyun/Speech/Config/IseConfig.php
Normal file
265
extend/IFlytek/Xfyun/Speech/Config/IseConfig.php
Normal file
@ -0,0 +1,265 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Config;
|
||||||
|
|
||||||
|
use IFlytek\Xfyun\Core\Traits\ArrayTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Traits\JsonTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Config\ConfigInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 语音评测配置参数类
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class IseConfig implements ConfigInterface
|
||||||
|
{
|
||||||
|
use ArrayTrait;
|
||||||
|
use JsonTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 服务类型指定
|
||||||
|
* ise: 开放评测
|
||||||
|
* 默认'ise'
|
||||||
|
*/
|
||||||
|
private $sub;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 评测语种
|
||||||
|
* 中文:cn_vip,英文:en_vip
|
||||||
|
* 默认'cn_vip'
|
||||||
|
*/
|
||||||
|
private $ent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 评测题型
|
||||||
|
* 中文题型:
|
||||||
|
* read_syllable(单字朗读,汉语专有)
|
||||||
|
* read_word(词语朗读)
|
||||||
|
* read_sentence(句子朗读)
|
||||||
|
* read_chapter(篇章朗读)
|
||||||
|
* 英文题型:
|
||||||
|
* read_word(词语朗读)
|
||||||
|
* read_sentence(句子朗读)
|
||||||
|
* read_chapter(篇章朗读)
|
||||||
|
* simple_expression(英文情景反应)
|
||||||
|
* read_choice(英文选择题)
|
||||||
|
* topic(英文自由题)
|
||||||
|
* retell(英文复述题)
|
||||||
|
* picture_talk(英文看图说话)
|
||||||
|
* oral_translation(英文口头翻译)
|
||||||
|
* 默认read_sentence
|
||||||
|
*/
|
||||||
|
private $category;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int 上传音频时来区分音频的状态
|
||||||
|
* 1/2/4
|
||||||
|
* 默认1
|
||||||
|
*/
|
||||||
|
private $aus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 用来区分数据上传阶段
|
||||||
|
* ssb:参数上传阶段
|
||||||
|
* ttp:文本上传阶段(ttp_skip=true时该阶段可以跳过,直接使用text字段中的文本)
|
||||||
|
* auw:音频上传阶段
|
||||||
|
* 必传
|
||||||
|
*/
|
||||||
|
private $cmd;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 待评测文本
|
||||||
|
* 需要加utf8bom头:'\uFEFF' + text
|
||||||
|
*/
|
||||||
|
private $text;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 带评测文本编码
|
||||||
|
* utf-8/gbk
|
||||||
|
* 默认utf-8
|
||||||
|
*/
|
||||||
|
private $tte;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var boolean 跳过ttp直接使用text中的文本进行评测
|
||||||
|
* true/false
|
||||||
|
* 默认true
|
||||||
|
*/
|
||||||
|
private $ttpSkip;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 拓展能力(生效条件ise_unite="1", rst="entirety")
|
||||||
|
* 多维度分信息显示(准确度分、流畅度分、完整度打分):extra_ability值为multi_dimension,字词句篇均适用,如选多个能力,用分号;隔开。例如:extra_ability=syll_phone_err_msg;pitch;multi_dimension
|
||||||
|
* 单词基频信息显示(基频开始值、结束值):extra_ability值为pitch ,仅适用于单词和句子题型
|
||||||
|
* 音素错误信息显示(声韵、调型是否正确):extra_ability值为syll_phone_err_msg,字词句篇均适用,如选多个能力,用分号;隔开。例如:extra_ability=syll_phone_err_msg;pitch;multi_dimension
|
||||||
|
*/
|
||||||
|
private $extraAbility;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 音频格式
|
||||||
|
* raw: 未压缩的pcm格式音频或wav(如果用wav格式音频,建议去掉头部)
|
||||||
|
* lame: mp3格式音频
|
||||||
|
* speex-wb;7: 讯飞定制speex格式音频
|
||||||
|
* 默认speex-wb
|
||||||
|
*/
|
||||||
|
private $aue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 音频采样率
|
||||||
|
* 默认 audio/L16;rate=16000
|
||||||
|
*/
|
||||||
|
private $auf;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 返回结果格式
|
||||||
|
* utf8/gbk
|
||||||
|
* 默认utf8
|
||||||
|
*/
|
||||||
|
private $rstcd;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 评测人群指定
|
||||||
|
* adult(成人群体,不设置群体参数时默认为成人)
|
||||||
|
* youth(中学群体)
|
||||||
|
* pupil(小学群体,中文句、篇题型设置此参数值会有accuracy_score得分的返回)
|
||||||
|
* 默认adult
|
||||||
|
*/
|
||||||
|
private $group;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 设置评测的打分及检错松严门限(仅中文引擎支持)
|
||||||
|
* easy:容易
|
||||||
|
* common:普通
|
||||||
|
* hard:困难
|
||||||
|
*/
|
||||||
|
private $checkType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 设置评测的学段参数 (仅中文题型:中小学的句子、篇章题型支持)
|
||||||
|
* junior(1,2年级)
|
||||||
|
* middle(3,4年级)
|
||||||
|
* senior(5,6年级)
|
||||||
|
*/
|
||||||
|
private $grade;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 评测返回结果与分制控制(评测返回结果与分制控制也会受到ise_unite与plev参数的影响)
|
||||||
|
* 完整:entirety(默认值)
|
||||||
|
* 中文百分制推荐传参(rst="entirety"且ise_unite="1"且配合extra_ability参数使用)
|
||||||
|
* 英文百分制推荐传参(rst="entirety"且ise_unite="1"且配合extra_ability参数使用)
|
||||||
|
* 精简:plain(评测返回结果将只有总分)
|
||||||
|
*/
|
||||||
|
private $rst;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 返回结果控制
|
||||||
|
* 0:不控制(默认值)
|
||||||
|
* 1:控制(extra_ability参数将影响全维度等信息的返回)
|
||||||
|
*/
|
||||||
|
private $iseUnite;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 在rst="entirety"(默认值)且ise_unite="0"(默认值)的情况下plev的取值不同对返回结果有影响。
|
||||||
|
* plev:0(给出全部信息,汉语包含rec_node_type、perr_msg、fluency_score、phone_score信息的返回;英文包含accuracy_score、serr_msg、 syll_accent、fluency_score、standard_score、pitch信息的返回)
|
||||||
|
*/
|
||||||
|
private $plev;
|
||||||
|
|
||||||
|
public function __construct($config)
|
||||||
|
{
|
||||||
|
$config += [
|
||||||
|
'ent' => 'cn_vip',
|
||||||
|
'category' => 'read_sentence',
|
||||||
|
'aus' => 1,
|
||||||
|
'cmd' => 'ssb',
|
||||||
|
'text' => '',
|
||||||
|
'tte' => 'utf-8',
|
||||||
|
'ttp_skip' => true,
|
||||||
|
'extra_ability' => null,
|
||||||
|
'aue' => 'raw',
|
||||||
|
'rstcd' => 'utf8',
|
||||||
|
'group' => 'adult',
|
||||||
|
'check_type' => 'common',
|
||||||
|
'grade' => 'middle',
|
||||||
|
'rst' => 'entirety',
|
||||||
|
'ise_unite' => '0',
|
||||||
|
'plev' => '0'
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->sub = 'ise';
|
||||||
|
$this->ent = $config['ent'];
|
||||||
|
$this->category = $config['category'];
|
||||||
|
$this->aus = $config['aus'];
|
||||||
|
$this->cmd = $config['cmd'];
|
||||||
|
$this->text = chr(239) . chr(187) . chr(191) . $config['text'];
|
||||||
|
$this->tte = $config['tte'];
|
||||||
|
$this->ttpSkip = $config['ttp_skip'];
|
||||||
|
$this->extraAbility = $config['extra_ability'];
|
||||||
|
$this->aue = $config['aue'];
|
||||||
|
$this->rstcd = $config['rstcd'];
|
||||||
|
$this->group = $config['group'];
|
||||||
|
$this->checkType = $config['check_type'];
|
||||||
|
$this->grade = $config['grade'];
|
||||||
|
$this->rst = $config['rst'];
|
||||||
|
$this->iseUnite = $config['ise_unite'];
|
||||||
|
$this->plev = $config['plev'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 去除null项后返回数组形式
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toArray()
|
||||||
|
{
|
||||||
|
return $this->removeNull([
|
||||||
|
'sub' => $this->sub,
|
||||||
|
'ent' => $this->ent,
|
||||||
|
'category' => $this->category,
|
||||||
|
'aus' => $this->aus,
|
||||||
|
'cmd' => $this->cmd,
|
||||||
|
'text' => $this->text,
|
||||||
|
'tte' => $this->tte,
|
||||||
|
'ttp_skip' => $this->ttpSkip,
|
||||||
|
'extra_ability' => $this->extraAbility,
|
||||||
|
'aue' => $this->aue,
|
||||||
|
'rstcd' => $this->rstcd,
|
||||||
|
'group' => $this->group,
|
||||||
|
'check_type' => $this->checkType,
|
||||||
|
'grade' => $this->grade,
|
||||||
|
'rst' => $this->rst,
|
||||||
|
'ise_unite' => $this->iseUnite,
|
||||||
|
'plev' => $this->plev
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回toArray的Json格式
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function toJson()
|
||||||
|
{
|
||||||
|
return $this->jsonEncode($this->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setText($text)
|
||||||
|
{
|
||||||
|
$this->text = chr(239) . chr(187) . chr(191) . $text;
|
||||||
|
}
|
||||||
|
}
|
142
extend/IFlytek/Xfyun/Speech/Config/LfasrConfig.php
Normal file
142
extend/IFlytek/Xfyun/Speech/Config/LfasrConfig.php
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Config;
|
||||||
|
|
||||||
|
use IFlytek\Xfyun\Core\Traits\ArrayTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Traits\JsonTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Config\ConfigInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 语音转写配置参数类
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class LfasrConfig implements ConfigInterface
|
||||||
|
{
|
||||||
|
use ArrayTrait;
|
||||||
|
use JsonTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 转写类型
|
||||||
|
* 0: (标准版,格式: wav,flac,opus,mp3,m4a)
|
||||||
|
* 默认: 0
|
||||||
|
*/
|
||||||
|
private $lfasrType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 转写结果是否包含分词信息
|
||||||
|
* false或true
|
||||||
|
* 默认false
|
||||||
|
*/
|
||||||
|
private $hasParticiple;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 转写结果中最大的候选词个数
|
||||||
|
* 0-5
|
||||||
|
* 默认0
|
||||||
|
*/
|
||||||
|
private $maxAlternatives;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 发音人个数
|
||||||
|
* 0-10
|
||||||
|
* 默认2
|
||||||
|
*/
|
||||||
|
private $speakerNumber;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 是否包含发音人分离信息
|
||||||
|
* false或true
|
||||||
|
* 默认false
|
||||||
|
*/
|
||||||
|
private $hasSeperate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 角色分离类型
|
||||||
|
* 1: 通用角色分离;2: 电话信道角色分离(适用于speaker_number为2的说话场景)
|
||||||
|
* 默认1,该字段只有在开通了角色分离功能的前提下才会生效,正确传入该参数后角色分离效果会有所提升。
|
||||||
|
*/
|
||||||
|
private $roleType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 语种
|
||||||
|
* cn: 中英文&中文;en: 英文(英文不支持热词)
|
||||||
|
* 默认cn
|
||||||
|
*/
|
||||||
|
private $language;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 垂直领域个性化参数
|
||||||
|
* court: 法院; edu: 教育; finance: 金融; medical: 医疗; tech: 科技;
|
||||||
|
* 默认通用
|
||||||
|
*/
|
||||||
|
private $pd;
|
||||||
|
|
||||||
|
public function __construct($config)
|
||||||
|
{
|
||||||
|
$config += [
|
||||||
|
'lfasr_type' => '0',
|
||||||
|
'has_participle' => 'false',
|
||||||
|
'max_alternatives' => '0',
|
||||||
|
'speaker_number' => '2',
|
||||||
|
'has_seperate' => 'false',
|
||||||
|
'role_type' => '1',
|
||||||
|
'language' => 'cn',
|
||||||
|
'pd' => null
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->lfasrType = $config['lfasr_type'];
|
||||||
|
$this->hasParticiple = $config['has_participle'];
|
||||||
|
$this->maxAlternatives = $config['max_alternatives'];
|
||||||
|
$this->speakerNumber = $config['speaker_number'];
|
||||||
|
$this->hasSeperate = $config['has_seperate'];
|
||||||
|
$this->roleType = $config['role_type'];
|
||||||
|
$this->language = $config['language'];
|
||||||
|
$this->pd = $config['pd'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 去除null项后返回数组形式
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toArray()
|
||||||
|
{
|
||||||
|
return $this->removeNull([
|
||||||
|
'lfasr_type' => $this->lfasrType,
|
||||||
|
'has_participle' => $this->hasParticiple,
|
||||||
|
'max_alternatives' => $this->maxAlternatives,
|
||||||
|
'speaker_number' => $this->speakerNumber,
|
||||||
|
'has_seperate' => $this->hasSeperate,
|
||||||
|
'role_type' => $this->roleType,
|
||||||
|
'language' => $this->language,
|
||||||
|
'pd' => $this->pd
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回toArray的Json格式
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function toJson()
|
||||||
|
{
|
||||||
|
return $this->jsonEncode($this->toArray());
|
||||||
|
}
|
||||||
|
}
|
76
extend/IFlytek/Xfyun/Speech/Config/TcConfig.php
Normal file
76
extend/IFlytek/Xfyun/Speech/Config/TcConfig.php
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Config;
|
||||||
|
|
||||||
|
use IFlytek\Xfyun\Core\Traits\ArrayTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Config\ConfigInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文本纠错配置参数类
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class TcConfig implements ConfigInterface
|
||||||
|
{
|
||||||
|
use ArrayTrait;
|
||||||
|
|
||||||
|
private $resultEncoding;
|
||||||
|
private $resultCompress;
|
||||||
|
private $resultFormat;
|
||||||
|
|
||||||
|
private $inputEncoding;
|
||||||
|
private $inputCompress;
|
||||||
|
private $inputFormat;
|
||||||
|
|
||||||
|
public function __construct($config)
|
||||||
|
{
|
||||||
|
$config += [
|
||||||
|
'resultEncoding' => 'utf8',
|
||||||
|
'resultCompress' => 'raw',
|
||||||
|
'resultFormat' => 'json',
|
||||||
|
'inputEncoding' => 'utf8',
|
||||||
|
'inputCompress' => 'raw',
|
||||||
|
'inputFormat' => 'json'
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->resultEncoding = $config['resultEncoding'];
|
||||||
|
$this->resultCompress = $config['resultCompress'];
|
||||||
|
$this->resultFormat = $config['resultFormat'];
|
||||||
|
$this->inputEncoding = $config['inputEncoding'];
|
||||||
|
$this->inputCompress = $config['inputCompress'];
|
||||||
|
$this->inputFormat = $config['inputFormat'];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toJson()
|
||||||
|
{
|
||||||
|
// TODO: Implement toJson() method.
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toArray()
|
||||||
|
{
|
||||||
|
return self::removeNull([
|
||||||
|
'resultEncoding' => $this->resultEncoding,
|
||||||
|
'resultCompress' => $this->resultCompress,
|
||||||
|
'resultFormat' => $this->resultFormat,
|
||||||
|
'inputEncoding' => $this->inputEncoding,
|
||||||
|
'inputCompress' => $this->inputCompress,
|
||||||
|
'inputFormat' => $this->inputFormat
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
191
extend/IFlytek/Xfyun/Speech/Config/TtsConfig.php
Normal file
191
extend/IFlytek/Xfyun/Speech/Config/TtsConfig.php
Normal file
@ -0,0 +1,191 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Config;
|
||||||
|
|
||||||
|
use IFlytek\Xfyun\Core\Traits\ArrayTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Traits\JsonTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Config\ConfigInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 语音合成配置参数类
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class TtsConfig implements ConfigInterface
|
||||||
|
{
|
||||||
|
use ArrayTrait;
|
||||||
|
use JsonTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 音频编码(必填)
|
||||||
|
* raw:未压缩的pcm
|
||||||
|
* lame:mp3 (当aue=lame时需传参sfl=1)
|
||||||
|
* speex-org-wb;7: 标准开源speex(for speex_wideband,即16k)数字代表指定压缩等级(默认等级为8)
|
||||||
|
* speex-org-nb;7: 标准开源speex(for speex_narrowband,即8k)数字代表指定压缩等级(默认等级为8)
|
||||||
|
* speex;7:压缩格式,压缩等级1~10,默认为7(8k讯飞定制speex)
|
||||||
|
* speex-wb;7:压缩格式,压缩等级1~10,默认为7(16k讯飞定制speex)
|
||||||
|
* 本sdk将默认采用raw格式
|
||||||
|
*/
|
||||||
|
private $aue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int 开启流式返回
|
||||||
|
* 需要配合aue=lame使用,开启流式返回mp3格式音频取值:1
|
||||||
|
*/
|
||||||
|
private $sfl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 音频采样率
|
||||||
|
* audio/L16;rate=8000: 合成8K的音频
|
||||||
|
* audio/L16;rate=16000: 合成16K的音频
|
||||||
|
* auf不传值: 合成16K的音频
|
||||||
|
*/
|
||||||
|
private $auf;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 发音人(必填)
|
||||||
|
* 可选值: 请到控制台添加试用或购买
|
||||||
|
* 默认xiaoyan
|
||||||
|
*/
|
||||||
|
private $vcn;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int 语速
|
||||||
|
* 可选值: 0-100
|
||||||
|
* 默认为50
|
||||||
|
*/
|
||||||
|
private $speed;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int 音量
|
||||||
|
* 可选值: 0-100
|
||||||
|
* 默认为50
|
||||||
|
*/
|
||||||
|
private $volume;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int 音高
|
||||||
|
* 可选值: 0-100
|
||||||
|
* 默认为50
|
||||||
|
*/
|
||||||
|
private $pitch;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int 合成音频的背景音
|
||||||
|
* 0: 无背景音; 1: 有背景音
|
||||||
|
* 默认0
|
||||||
|
*/
|
||||||
|
private $bgs;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 文本编码格式
|
||||||
|
* GB2312|GBK|BIG5|UNICODE|GB18030|UTF8
|
||||||
|
* 默认UTF-8
|
||||||
|
*/
|
||||||
|
private $tte;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 设置英文发音方式
|
||||||
|
* 0: 自动判断处理,如果不确定将按照英文词语拼写处理
|
||||||
|
* 1: 所有英文按字母发音
|
||||||
|
* 2: 自动判断处理,如果不确定将按照字母朗读
|
||||||
|
* 默认2
|
||||||
|
*/
|
||||||
|
private $reg;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 合成音频数字发音方式
|
||||||
|
* 0: 自动判断
|
||||||
|
* 1: 完全数值
|
||||||
|
* 2: 完全字符串
|
||||||
|
* 3: 字符串优先
|
||||||
|
* 默认0
|
||||||
|
*/
|
||||||
|
private $rdn;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string 自定义发音人引擎
|
||||||
|
* 默认传 ptts
|
||||||
|
*/
|
||||||
|
private $ent;
|
||||||
|
|
||||||
|
public function __construct($config = [])
|
||||||
|
{
|
||||||
|
$config += [
|
||||||
|
'aue' => 'lame',
|
||||||
|
'sfl' => 1,
|
||||||
|
'auf' => null,
|
||||||
|
'vcn' => 'xiaoyan',
|
||||||
|
'speed' => 50,
|
||||||
|
'volume' => 50,
|
||||||
|
'pitch' => 50,
|
||||||
|
'bgs' => 0,
|
||||||
|
'tte' => 'UTF8',
|
||||||
|
'reg' => '2',
|
||||||
|
'rdn' => '0',
|
||||||
|
'ent' => ''
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->aue = $config['aue'];
|
||||||
|
$this->sfl = $config['sfl'];
|
||||||
|
$this->auf = $config['auf'];
|
||||||
|
$this->vcn = $config['vcn'];
|
||||||
|
$this->speed = $config['speed'];
|
||||||
|
$this->volume = $config['volume'];
|
||||||
|
$this->pitch = $config['pitch'];
|
||||||
|
$this->bgs = $config['bgs'];
|
||||||
|
$this->tte = $config['tte'];
|
||||||
|
$this->reg = $config['reg'];
|
||||||
|
$this->rdn = $config['rdn'];
|
||||||
|
$this->ent = $config['ent'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 去除null项后返回数组形式
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toArray()
|
||||||
|
{
|
||||||
|
return $this->removeNull([
|
||||||
|
'aue' => $this->aue,
|
||||||
|
'sfl' => $this->sfl,
|
||||||
|
'auf' => $this->auf,
|
||||||
|
'vcn' => $this->vcn,
|
||||||
|
'speed' => $this->speed,
|
||||||
|
'volume' => $this->volume,
|
||||||
|
'pitch' => $this->pitch,
|
||||||
|
'bgs' => $this->bgs,
|
||||||
|
'tte' => $this->tte,
|
||||||
|
'reg' => $this->reg,
|
||||||
|
'rdn' => $this->rdn,
|
||||||
|
'ent' => $this->ent
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回toArray的Json格式
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function toJson()
|
||||||
|
{
|
||||||
|
return $this->jsonEncode($this->toArray());
|
||||||
|
}
|
||||||
|
}
|
31
extend/IFlytek/Xfyun/Speech/Constants/IgrConstants.php
Normal file
31
extend/IFlytek/Xfyun/Speech/Constants/IgrConstants.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Constants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 性别年龄识别常量
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class IgrConstants
|
||||||
|
{
|
||||||
|
const URI = 'wss://ws-api.xfyun.cn/v2/igr';
|
||||||
|
const HOST = 'ws-api.xfyun.cn';
|
||||||
|
const REQUEST_LINE = 'GET /v2/igr HTTP/1.1';
|
||||||
|
const FRAME_SIZE = 1280;
|
||||||
|
}
|
31
extend/IFlytek/Xfyun/Speech/Constants/IseConstants.php
Normal file
31
extend/IFlytek/Xfyun/Speech/Constants/IseConstants.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Constants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 语音评测常量
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class IseConstants
|
||||||
|
{
|
||||||
|
const URI = 'wss://ise-api.xfyun.cn/v2/open-ise';
|
||||||
|
const HOST = 'ise-api.xfyun.cn';
|
||||||
|
const REQUEST_LINE = 'GET /v2/open-ise HTTP/1.1';
|
||||||
|
const FRAME_SIZE = 1280;
|
||||||
|
}
|
34
extend/IFlytek/Xfyun/Speech/Constants/LfasrConstants.php
Normal file
34
extend/IFlytek/Xfyun/Speech/Constants/LfasrConstants.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Constants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 语音转写常量
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class LfasrConstants
|
||||||
|
{
|
||||||
|
const URI_PREPARE = 'https://raasr.xfyun.cn/api/prepare';
|
||||||
|
const URI_UPLOAD = 'https://raasr.xfyun.cn/api/upload';
|
||||||
|
const URI_MERGE = 'https://raasr.xfyun.cn/api/merge';
|
||||||
|
const URI_GET_PROGRESS = 'https://raasr.xfyun.cn/api/getProgress';
|
||||||
|
const URI_GET_RESULT = 'https://raasr.xfyun.cn/api/getResult';
|
||||||
|
const SLICE_PIECE_SIZE = 1024 * 1024 * 10;
|
||||||
|
const ORIGIN_SLICE_ID = 'aaaaaaaaaa';
|
||||||
|
}
|
31
extend/IFlytek/Xfyun/Speech/Constants/TcConstants.php
Normal file
31
extend/IFlytek/Xfyun/Speech/Constants/TcConstants.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Constants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文本纠错常量
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class TcConstants
|
||||||
|
{
|
||||||
|
const URI = 'https://api.xf-yun.com/v1/private/s9a87e3ec';
|
||||||
|
const LIST_UPLOAD_URI = 'https://evo-gen.xfyun.cn/individuation/gen/upload';
|
||||||
|
const HOST = 'api.xf-yun.com';
|
||||||
|
const REQUEST_LINE = 'POST /v1/private/s9a87e3ec HTTP/1.1';
|
||||||
|
}
|
30
extend/IFlytek/Xfyun/Speech/Constants/TtsConstants.php
Normal file
30
extend/IFlytek/Xfyun/Speech/Constants/TtsConstants.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Constants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 语音合成常量
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class TtsConstants
|
||||||
|
{
|
||||||
|
const URI = 'wss://tts-api.xfyun.cn/v2/tts';
|
||||||
|
const HOST = 'tts-api.xfyun.cn';
|
||||||
|
const REQUEST_LINE = 'GET /v2/tts HTTP/1.1';
|
||||||
|
}
|
62
extend/IFlytek/Xfyun/Speech/Helper/SliceIdGenerator.php
Normal file
62
extend/IFlytek/Xfyun/Speech/Helper/SliceIdGenerator.php
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Helper;
|
||||||
|
|
||||||
|
use IFlytek\Xfyun\Speech\Constants\LfasrConstants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转写切片ID生成类
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class SliceIdGenerator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string 当前切片ID
|
||||||
|
*/
|
||||||
|
private $id;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->id = LfasrConstants::ORIGIN_SLICE_ID;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回当前切片ID,并生成下一个切片ID,赋值给对象的当前ID
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getId()
|
||||||
|
{
|
||||||
|
$currentId = $this->id;
|
||||||
|
$nextId = $currentId;
|
||||||
|
$pos = strlen($currentId) - 1;
|
||||||
|
while ($pos >= 0) {
|
||||||
|
$charAtPos = $nextId[$pos];
|
||||||
|
if ($charAtPos != 'z') {
|
||||||
|
$nextId = substr($nextId, 0, $pos) . chr((ord($charAtPos) + 1)) . substr($nextId, $pos + 1);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
$nextId = substr($nextId, 0, $pos) . 'a';
|
||||||
|
$pos = $pos - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->id = $nextId;
|
||||||
|
return $currentId;
|
||||||
|
}
|
||||||
|
}
|
111
extend/IFlytek/Xfyun/Speech/IgrClient.php
Normal file
111
extend/IFlytek/Xfyun/Speech/IgrClient.php
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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 Exception;
|
||||||
|
use IFlytek\Xfyun\Speech\Config\IgrConfig;
|
||||||
|
use IFlytek\Xfyun\Speech\Constants\IgrConstants;
|
||||||
|
use IFlytek\Xfyun\Speech\Traits\IgrTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Handler\WsHandler;
|
||||||
|
use IFlytek\Xfyun\Core\WsClient;
|
||||||
|
use IFlytek\Xfyun\Core\Traits\SignTrait;
|
||||||
|
use GuzzleHttp\Psr7\Stream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 性别年龄识别客户端
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class IgrClient
|
||||||
|
{
|
||||||
|
use SignTrait;
|
||||||
|
use IgrTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string app_id
|
||||||
|
*/
|
||||||
|
protected $appId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string api_key
|
||||||
|
*/
|
||||||
|
protected $apiKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string api_secret
|
||||||
|
*/
|
||||||
|
protected $apiSecret;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IgrConfig
|
||||||
|
*/
|
||||||
|
protected $requestConfig;
|
||||||
|
|
||||||
|
public function __construct($appId, $apiKey, $apiSecret, $requestConfig = [])
|
||||||
|
{
|
||||||
|
$this->appId = $appId;
|
||||||
|
$this->apiKey = $apiKey;
|
||||||
|
$this->apiSecret = $apiSecret;
|
||||||
|
$this->requestConfig = new IgrConfig($requestConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求并返回结果
|
||||||
|
*
|
||||||
|
* @param string $audioPath 待识别音频路径
|
||||||
|
* @return string
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function request($audioPath)
|
||||||
|
{
|
||||||
|
$ttsHandler = new WsHandler(
|
||||||
|
$this->signUriV1(IgrConstants::URI, [
|
||||||
|
'appId' => $this->appId,
|
||||||
|
'apiKey' => $this->apiKey,
|
||||||
|
'apiSecret' => $this->apiSecret,
|
||||||
|
'host' => IgrConstants::HOST,
|
||||||
|
'requestLine' => IgrConstants::REQUEST_LINE,
|
||||||
|
]),
|
||||||
|
null
|
||||||
|
);
|
||||||
|
$client = new WsClient([
|
||||||
|
'handler' => $ttsHandler
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 音频上传
|
||||||
|
$frameNum = ceil(fileSize($audioPath) / IgrConstants::FRAME_SIZE);
|
||||||
|
$fileStream = new Stream(fopen($audioPath, 'r'));
|
||||||
|
// 发送第一帧
|
||||||
|
$client->send($this->generateAudioInput($fileStream->read(IgrConstants::FRAME_SIZE), true, false));
|
||||||
|
|
||||||
|
// 发送中间帧
|
||||||
|
for ($i = 1; $i < $frameNum; $i++) {
|
||||||
|
$client->send($this->generateAudioInput($fileStream->read(IgrConstants::FRAME_SIZE), false, false));
|
||||||
|
usleep(4000);
|
||||||
|
}
|
||||||
|
// 发送最后一帧
|
||||||
|
$client->send($this->generateAudioInput('', false, true));
|
||||||
|
|
||||||
|
// 接受数据
|
||||||
|
$message = $this->jsonDecode($client->receive(), true);
|
||||||
|
if ($message['code'] !== 0) {
|
||||||
|
throw new Exception(json_encode($message));
|
||||||
|
}
|
||||||
|
return $message['data'];
|
||||||
|
}
|
||||||
|
}
|
126
extend/IFlytek/Xfyun/Speech/IseClient.php
Normal file
126
extend/IFlytek/Xfyun/Speech/IseClient.php
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Speech\Config\IseConfig;
|
||||||
|
use IFlytek\Xfyun\Speech\Constants\IseConstants;
|
||||||
|
use IFlytek\Xfyun\Speech\Traits\IseTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Handler\WsHandler;
|
||||||
|
use IFlytek\Xfyun\Core\WsClient;
|
||||||
|
use IFlytek\Xfyun\Core\Traits\SignTrait;
|
||||||
|
use GuzzleHttp\Psr7\Stream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 语音评测客户端
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class IseClient
|
||||||
|
{
|
||||||
|
use SignTrait;
|
||||||
|
use IseTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string app_id
|
||||||
|
*/
|
||||||
|
protected $appId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string api_key
|
||||||
|
*/
|
||||||
|
protected $apiKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string api_secret
|
||||||
|
*/
|
||||||
|
protected $apiSecret;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array 评测参数配置
|
||||||
|
*/
|
||||||
|
protected $requestConfig;
|
||||||
|
|
||||||
|
public function __construct($appId, $apiKey, $apiSecret, $requestConfig = [])
|
||||||
|
{
|
||||||
|
$this->appId = $appId;
|
||||||
|
$this->apiKey = $apiKey;
|
||||||
|
$this->apiSecret = $apiSecret;
|
||||||
|
$this->requestConfig = new IseConfig($requestConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求评测,并返回结果(xml格式)
|
||||||
|
*
|
||||||
|
* @param string $audioPath 待评测音频路径
|
||||||
|
* @param string $text 待评测文本
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function request($audioPath, $text = null)
|
||||||
|
{
|
||||||
|
$ttsHandler = new WsHandler(
|
||||||
|
$this->signUriV1(IseConstants::URI, [
|
||||||
|
'appId' => $this->appId,
|
||||||
|
'apiKey' => $this->apiKey,
|
||||||
|
'apiSecret' => $this->apiSecret,
|
||||||
|
'host' => IseConstants::HOST,
|
||||||
|
'requestLine' => IseConstants::REQUEST_LINE,
|
||||||
|
]),
|
||||||
|
null
|
||||||
|
);
|
||||||
|
$client = new WsClient([
|
||||||
|
'handler' => $ttsHandler
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 参数上传
|
||||||
|
if (!empty($text)) {
|
||||||
|
$this->requestConfig->setText($text);
|
||||||
|
}
|
||||||
|
$client->send($this->generateParamsInput($this->appId, $this->requestConfig->toArray()));
|
||||||
|
|
||||||
|
// 音频上传
|
||||||
|
$frameSize = ceil(fileSize($audioPath) / IseConstants::FRAME_SIZE);
|
||||||
|
$fileStream = new Stream(fopen($audioPath, 'r'));
|
||||||
|
// 发送第一帧
|
||||||
|
$result = $client->send($this->generateAudioInput($fileStream->read(IseConstants::FRAME_SIZE), true, false));
|
||||||
|
// 发送中间帧
|
||||||
|
for ($i = 1; $i < $frameSize - 1; $i++) {
|
||||||
|
$client->send($this->generateAudioInput($fileStream->read(IseConstants::FRAME_SIZE), false, false));
|
||||||
|
usleep(4000);
|
||||||
|
}
|
||||||
|
// 发送最后一帧
|
||||||
|
$client->send($this->generateAudioInput($fileStream->read(IseConstants::FRAME_SIZE), false, true));
|
||||||
|
|
||||||
|
// 接受数据
|
||||||
|
$result = '';
|
||||||
|
while (true) {
|
||||||
|
$message = $this->jsonDecode($client->receive());
|
||||||
|
if ($message->code !== 0) {
|
||||||
|
throw new \Exception(json_encode($message));
|
||||||
|
}
|
||||||
|
switch ($message->data->status) {
|
||||||
|
case 1:
|
||||||
|
$result .= base64_decode($message->data->data);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
$result .= base64_decode($message->data->data);
|
||||||
|
break 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
231
extend/IFlytek/Xfyun/Speech/LfasrClient.php
Normal file
231
extend/IFlytek/Xfyun/Speech/LfasrClient.php
Normal file
@ -0,0 +1,231 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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 Exception;
|
||||||
|
use IFlytek\Xfyun\Speech\Config\LfasrConfig;
|
||||||
|
use IFlytek\Xfyun\Speech\Constants\LfasrConstants;
|
||||||
|
use IFlytek\Xfyun\Speech\Traits\LfasrTrait;
|
||||||
|
use IFlytek\Xfyun\Speech\Helper\SliceIdGenerator;
|
||||||
|
use IFlytek\Xfyun\Core\Traits\SignTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Traits\JsonTrait;
|
||||||
|
use IFlytek\Xfyun\Core\HttpClient;
|
||||||
|
use GuzzleHttp\Psr7\Request;
|
||||||
|
use GuzzleHttp\Psr7\Response;
|
||||||
|
use GuzzleHttp\Psr7\Query;
|
||||||
|
use GuzzleHttp\Psr7\Stream;
|
||||||
|
use GuzzleHttp\Psr7\MultipartStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 语音转写客户端
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class LfasrClient
|
||||||
|
{
|
||||||
|
use SignTrait;
|
||||||
|
use JsonTrait;
|
||||||
|
use LfasrTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string app_id
|
||||||
|
*/
|
||||||
|
protected $appId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string secret_key
|
||||||
|
*/
|
||||||
|
protected $secretKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array 转写参数配置
|
||||||
|
*/
|
||||||
|
protected $requestConfig;
|
||||||
|
|
||||||
|
/** @var HttpClient */
|
||||||
|
protected $client;
|
||||||
|
|
||||||
|
/** @var array 初始化请求体 */
|
||||||
|
protected $requestBody;
|
||||||
|
|
||||||
|
public function __construct($appId, $secretKey, $requestConfig = [])
|
||||||
|
{
|
||||||
|
$this->appId = $appId;
|
||||||
|
$this->secretKey = $secretKey;
|
||||||
|
$this->requestConfig = new LfasrConfig($requestConfig);
|
||||||
|
$this->client = new HttpClient([]);
|
||||||
|
$timestamp = time();
|
||||||
|
$this->requestBody = [
|
||||||
|
'app_id' => $this->appId,
|
||||||
|
'signa' => $this->signV1($this->appId, $this->secretKey, $timestamp),
|
||||||
|
'ts' => $timestamp,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打包上传接口,封装了准备、分片上传和合并三个接口,并返回task_id
|
||||||
|
*
|
||||||
|
* @param string $filePath 文件路径
|
||||||
|
* @return string
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function combineUpload($filePath)
|
||||||
|
{
|
||||||
|
$prepareResponse = $this->prepare($filePath);
|
||||||
|
$prepareContent = $this->jsonDecode($prepareResponse->getBody()->getContents(), true);
|
||||||
|
$taskId = $prepareContent['data'];
|
||||||
|
$this->upload($taskId, $filePath);
|
||||||
|
$this->merge($taskId);
|
||||||
|
return $taskId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 准备接口
|
||||||
|
*
|
||||||
|
* @param $file_path
|
||||||
|
* @return Response
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function prepare($file_path)
|
||||||
|
{
|
||||||
|
$this->requestBody += $this->fileInfo($file_path);
|
||||||
|
$this->requestBody += $this->sliceInfo($file_path);
|
||||||
|
$this->requestBody += $this->requestConfig->toArray();
|
||||||
|
|
||||||
|
return $this->client->sendAndReceive(
|
||||||
|
new Request(
|
||||||
|
'POST',
|
||||||
|
LfasrConstants::URI_PREPARE,
|
||||||
|
['Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8'],
|
||||||
|
Query::build($this->requestBody)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分片上传接口
|
||||||
|
*
|
||||||
|
* @param string $taskId task_id
|
||||||
|
* @param string $filePath 文件路径
|
||||||
|
* @return Response
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function upload($taskId, $filePath)
|
||||||
|
{
|
||||||
|
$sliceIdGenerator = new SliceIdGenerator();
|
||||||
|
$sliceInfo = $this->sliceInfo($filePath);
|
||||||
|
$fileStream = new Stream(fopen($filePath, 'r'));
|
||||||
|
|
||||||
|
$this->requestBody += [
|
||||||
|
'task_id' => $taskId
|
||||||
|
];
|
||||||
|
|
||||||
|
$request = new Request(
|
||||||
|
'POST',
|
||||||
|
LfasrConstants::URI_UPLOAD
|
||||||
|
);
|
||||||
|
|
||||||
|
for ($i = 0; $i < $sliceInfo['slice_num']; $i++) {
|
||||||
|
$multipartStream = new MultipartStream([
|
||||||
|
[
|
||||||
|
'name' => 'app_id',
|
||||||
|
'contents' => $this->requestBody['app_id']
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => 'signa',
|
||||||
|
'contents' => $this->requestBody['signa']
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => 'ts',
|
||||||
|
'contents' => $this->requestBody['ts']
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => 'task_id',
|
||||||
|
'contents' => $taskId,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => 'slice_id',
|
||||||
|
'contents' => $sliceIdGenerator->getId()
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => 'content',
|
||||||
|
'contents' => $fileStream->read(LfasrConstants::SLICE_PIECE_SIZE),
|
||||||
|
'filename' => '1.pcm'
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->client->sendAndReceive(
|
||||||
|
$request->withBody($multipartStream)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return new Response(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 合并接口
|
||||||
|
*
|
||||||
|
* @param string $taskId task_id
|
||||||
|
* @return Response
|
||||||
|
*/
|
||||||
|
public function merge($taskId)
|
||||||
|
{
|
||||||
|
return $this->process(LfasrConstants::URI_MERGE, $taskId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询进度接口
|
||||||
|
*
|
||||||
|
* @param string $taskId task_id
|
||||||
|
* @return Response
|
||||||
|
*/
|
||||||
|
public function getProgress($taskId)
|
||||||
|
{
|
||||||
|
return $this->process(LfasrConstants::URI_GET_PROGRESS, $taskId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取结果接口
|
||||||
|
*
|
||||||
|
* @param string $taskId task_id
|
||||||
|
* @return Response
|
||||||
|
*/
|
||||||
|
public function getResult($taskId)
|
||||||
|
{
|
||||||
|
return $this->process(LfasrConstants::URI_GET_RESULT, $taskId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 封装操作
|
||||||
|
*
|
||||||
|
* @param string $task 操作
|
||||||
|
* @param string $taskId task_id
|
||||||
|
* @return Response
|
||||||
|
*/
|
||||||
|
private function process($task, $taskId)
|
||||||
|
{
|
||||||
|
$this->requestBody += ['task_id' => $taskId];
|
||||||
|
return $this->client->sendAndReceive(
|
||||||
|
new Request(
|
||||||
|
'POST',
|
||||||
|
$task,
|
||||||
|
['Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8'],
|
||||||
|
Query::build($this->requestBody)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
104
extend/IFlytek/Xfyun/Speech/TcClient.php
Normal file
104
extend/IFlytek/Xfyun/Speech/TcClient.php
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
<?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\Speech\Config\TcConfig;
|
||||||
|
use IFlytek\Xfyun\Speech\Constants\TcConstants;
|
||||||
|
use IFlytek\Xfyun\Speech\Traits\TcTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Traits\SignTrait;
|
||||||
|
use IFlytek\Xfyun\Core\HttpClient;
|
||||||
|
use GuzzleHttp\Psr7\Request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文本纠错客户端
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class TcClient
|
||||||
|
{
|
||||||
|
use SignTrait;
|
||||||
|
use TcTrait;
|
||||||
|
|
||||||
|
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->requestConfig = new TcConfig($requestConfig);
|
||||||
|
$this->client = new HttpClient([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function request($text)
|
||||||
|
{
|
||||||
|
$uri = self::signUriV1(TcConstants::URI, [
|
||||||
|
'apiKey' => $this->apiKey,
|
||||||
|
'apiSecret' => $this->apiSecret,
|
||||||
|
'host' => TcConstants::HOST,
|
||||||
|
'requestLine' => TcConstants::REQUEST_LINE
|
||||||
|
]);
|
||||||
|
$body = self::generateInput($text, $this->appId, $this->uid, $this->resId, $this->requestConfig->toArray());
|
||||||
|
return $this->client->sendAndReceive(
|
||||||
|
new Request(
|
||||||
|
'POST',
|
||||||
|
$uri,
|
||||||
|
['Content-Type' => 'application/json;charset=UTF-8'],
|
||||||
|
$body
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function listUpload($whiteList, $blackList)
|
||||||
|
{
|
||||||
|
if (empty($this->uid) || empty($this->resId)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$response = $this->client->sendAndReceive(
|
||||||
|
new Request(
|
||||||
|
'POST',
|
||||||
|
TcConstants::LIST_UPLOAD_URI,
|
||||||
|
['Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8'],
|
||||||
|
self::jsonEncode([
|
||||||
|
'common' => [
|
||||||
|
'app_id' => $this->appId,
|
||||||
|
'uid' => $this->uid,
|
||||||
|
|
||||||
|
],
|
||||||
|
'business' => [
|
||||||
|
'res_id' => $this->resId
|
||||||
|
],
|
||||||
|
'data' => base64_encode(self::jsonEncode([
|
||||||
|
'white_list' => $whiteList,
|
||||||
|
'black_list' => $blackList
|
||||||
|
]))
|
||||||
|
])
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$content = json_decode($response->getBody()->getContents(), true);
|
||||||
|
return $content['message'] == 'success';
|
||||||
|
}
|
||||||
|
}
|
59
extend/IFlytek/Xfyun/Speech/Traits/IgrTrait.php
Normal file
59
extend/IFlytek/Xfyun/Speech/Traits/IgrTrait.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Traits;
|
||||||
|
|
||||||
|
use IFlytek\Xfyun\Core\Traits\ArrayTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Traits\JsonTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 性别年龄识别方法
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
trait IgrTrait
|
||||||
|
{
|
||||||
|
use ArrayTrait;
|
||||||
|
use JsonTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据音频数据、是否是第一帧、最后一帧,生成音频上传请求体
|
||||||
|
*
|
||||||
|
* @param string $frameData 音频数据
|
||||||
|
* @param boolean $isFirstFrame 是否是第一帧
|
||||||
|
* @param boolean $isLastFrame 是否是最后一帧
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function generateAudioInput($frameData, $isFirstFrame = false, $isLastFrame = false)
|
||||||
|
{
|
||||||
|
return self::jsonEncode(
|
||||||
|
self::removeNull([
|
||||||
|
"common" => !$isFirstFrame ? null : [
|
||||||
|
"app_id" => $this->appId
|
||||||
|
],
|
||||||
|
'business' => !$isFirstFrame ? null : [
|
||||||
|
"aue" => $this->requestConfig->getAue(),
|
||||||
|
"rate" => $this->requestConfig->getRate()
|
||||||
|
],
|
||||||
|
'data' => [
|
||||||
|
'status' => $isFirstFrame ? 0 : ($isLastFrame ? 2 : 1),
|
||||||
|
'audio' => base64_encode($frameData)
|
||||||
|
]
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
78
extend/IFlytek/Xfyun/Speech/Traits/IseTrait.php
Normal file
78
extend/IFlytek/Xfyun/Speech/Traits/IseTrait.php
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Traits;
|
||||||
|
|
||||||
|
use IFlytek\Xfyun\Core\Traits\ArrayTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Traits\JsonTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 语音评测方法
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
trait IseTrait
|
||||||
|
{
|
||||||
|
use ArrayTrait;
|
||||||
|
use JsonTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据合成内容、app_id、配置参数,生成请求体
|
||||||
|
*
|
||||||
|
* @param string $appId app_id
|
||||||
|
* @param array $iseConfigArray 语音合成参数,详见iseConfig
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function generateParamsInput($appId, $iseConfigArray)
|
||||||
|
{
|
||||||
|
return self::jsonEncode(
|
||||||
|
self::removeNull([
|
||||||
|
'common' => [
|
||||||
|
'app_id' => $appId
|
||||||
|
],
|
||||||
|
'business' => $iseConfigArray,
|
||||||
|
'data' => [
|
||||||
|
'status' => 0
|
||||||
|
]
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据音频数据、是否是第一帧、最后一帧,生成音频上传请求体
|
||||||
|
*
|
||||||
|
* @param string $frameData 音频数据
|
||||||
|
* @param boolean $isFirstFrame 是否是第一帧
|
||||||
|
* @param boolean $isLastFrame 是否是最后一帧
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function generateAudioInput($frameData, $isFirstFrame = false, $isLastFrame = false)
|
||||||
|
{
|
||||||
|
return self::jsonEncode(
|
||||||
|
self::removeNull([
|
||||||
|
'business' => [
|
||||||
|
"cmd" => "auw",
|
||||||
|
"aus" => $isFirstFrame ? 1 : ($isLastFrame ? 4 : 2)
|
||||||
|
],
|
||||||
|
'data' => [
|
||||||
|
'status' => $isLastFrame ? 2 : 1,
|
||||||
|
'data' => base64_encode($frameData)
|
||||||
|
]
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
56
extend/IFlytek/Xfyun/Speech/Traits/LfasrTrait.php
Normal file
56
extend/IFlytek/Xfyun/Speech/Traits/LfasrTrait.php
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Traits;
|
||||||
|
|
||||||
|
use IFlytek\Xfyun\Speech\Constants\LfasrConstants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转写方法
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
trait LfasrTrait
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 获取文件名和文件大小
|
||||||
|
*
|
||||||
|
* @param string $filePath 文件路径
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function fileInfo($filePath)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'file_name' => basename($filePath),
|
||||||
|
'file_len' => filesize($filePath)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据文件大小和SDK默认分片大小,获取文件分片数目信息
|
||||||
|
*
|
||||||
|
* @param string $filePath 文件路径
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function sliceInfo($filePath)
|
||||||
|
{
|
||||||
|
$fileSize = filesize($filePath);
|
||||||
|
return [
|
||||||
|
'slice_num' => ceil($fileSize / LfasrConstants::SLICE_PIECE_SIZE)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
64
extend/IFlytek/Xfyun/Speech/Traits/TcTrait.php
Normal file
64
extend/IFlytek/Xfyun/Speech/Traits/TcTrait.php
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Traits;
|
||||||
|
|
||||||
|
use IFlytek\Xfyun\Core\Traits\ArrayTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Traits\JsonTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文本纠错方法
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
trait TcTrait
|
||||||
|
{
|
||||||
|
use ArrayTrait;
|
||||||
|
use JsonTrait;
|
||||||
|
|
||||||
|
public static function generateInput($text, $appId, $uid, $resId, $tcConfigArray)
|
||||||
|
{
|
||||||
|
return self::jsonEncode(
|
||||||
|
self::removeNull([
|
||||||
|
'header' => [
|
||||||
|
'app_id' => $appId,
|
||||||
|
'uid' => $uid,
|
||||||
|
'status' => 3
|
||||||
|
],
|
||||||
|
'parameter' => [
|
||||||
|
's9a87e3ec' => [
|
||||||
|
'res_id' => $resId,
|
||||||
|
'result' => [
|
||||||
|
'encoding' => $tcConfigArray['resultEncoding'],
|
||||||
|
'compress' => $tcConfigArray['resultCompress'],
|
||||||
|
'format' => $tcConfigArray['resultFormat'],
|
||||||
|
]
|
||||||
|
]
|
||||||
|
],
|
||||||
|
'payload' => [
|
||||||
|
'input' => [
|
||||||
|
'encoding' => $tcConfigArray['inputEncoding'],
|
||||||
|
'compress' => $tcConfigArray['inputCompress'],
|
||||||
|
'format' => $tcConfigArray['inputFormat'],
|
||||||
|
'status' => 3,
|
||||||
|
'text' => base64_encode($text)
|
||||||
|
]
|
||||||
|
]
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
56
extend/IFlytek/Xfyun/Speech/Traits/TtsTrait.php
Normal file
56
extend/IFlytek/Xfyun/Speech/Traits/TtsTrait.php
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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\Traits;
|
||||||
|
|
||||||
|
use IFlytek\Xfyun\Core\Traits\ArrayTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Traits\JsonTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 语音合成方法
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
trait TtsTrait
|
||||||
|
{
|
||||||
|
use ArrayTrait;
|
||||||
|
use JsonTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据合成内容、app_id、配置参数,生成请求体
|
||||||
|
*
|
||||||
|
* @param string $text 带合成的文本
|
||||||
|
* @param string $appId app_id
|
||||||
|
* @param array $ttsConfigArray 语音合成参数,详见TtsConfig
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function generateInput($text, $appId, $ttsConfigArray)
|
||||||
|
{
|
||||||
|
return self::jsonEncode(
|
||||||
|
self::removeNull([
|
||||||
|
'common' => [
|
||||||
|
'app_id' => $appId
|
||||||
|
],
|
||||||
|
'business' => $ttsConfigArray,
|
||||||
|
'data' => [
|
||||||
|
'text' => base64_encode($text),
|
||||||
|
'status' => 2
|
||||||
|
]
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
100
extend/IFlytek/Xfyun/Speech/TtsClient.php
Normal file
100
extend/IFlytek/Xfyun/Speech/TtsClient.php
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 1999-2021 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 Exception;
|
||||||
|
use IFlytek\Xfyun\Speech\Config\TtsConfig;
|
||||||
|
use IFlytek\Xfyun\Speech\Constants\TtsConstants;
|
||||||
|
use IFlytek\Xfyun\Speech\Traits\TtsTrait;
|
||||||
|
use IFlytek\Xfyun\Core\Handler\WsHandler;
|
||||||
|
use IFlytek\Xfyun\Core\WsClient;
|
||||||
|
use IFlytek\Xfyun\Core\Traits\SignTrait;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 语音合成客户端
|
||||||
|
*
|
||||||
|
* @author guizheng@iflytek.com
|
||||||
|
*/
|
||||||
|
class TtsClient
|
||||||
|
{
|
||||||
|
use SignTrait;
|
||||||
|
use TtsTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string app_id
|
||||||
|
*/
|
||||||
|
protected $appId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string api_key
|
||||||
|
*/
|
||||||
|
protected $apiKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string api_secret
|
||||||
|
*/
|
||||||
|
protected $apiSecret;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array 合成参数配置
|
||||||
|
*/
|
||||||
|
protected $requestConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var LoggerInterface or null 日志处理
|
||||||
|
*/
|
||||||
|
protected $logger;
|
||||||
|
|
||||||
|
public function __construct($appId, $apiKey, $apiSecret, $requestConfig = [], $logger = null)
|
||||||
|
{
|
||||||
|
$this->appId = $appId;
|
||||||
|
$this->apiKey = $apiKey;
|
||||||
|
$this->apiSecret = $apiSecret;
|
||||||
|
$this->requestConfig = new TtsConfig($requestConfig);
|
||||||
|
$this->logger = $logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 合成文本,并返回结果(字节数组)在Response->getBody()->getContents()
|
||||||
|
*
|
||||||
|
* @param string $text 待合成的文本
|
||||||
|
* @return \GuzzleHttp\Psr7\Response
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function request($text)
|
||||||
|
{
|
||||||
|
$ttsHandler = new WsHandler(
|
||||||
|
$this->signUriV1(TtsConstants::URI, [
|
||||||
|
'appId' => $this->appId,
|
||||||
|
'apiKey' => $this->apiKey,
|
||||||
|
'apiSecret' => $this->apiSecret,
|
||||||
|
'host' => TtsConstants::HOST,
|
||||||
|
'requestLine' => TtsConstants::REQUEST_LINE,
|
||||||
|
]),
|
||||||
|
$this->generateInput($text, $this->appId, $this->requestConfig->toArray())
|
||||||
|
);
|
||||||
|
if ($this->logger) {
|
||||||
|
$ttsHandler->setLogger($this->logger);
|
||||||
|
}
|
||||||
|
$client = new WsClient([
|
||||||
|
'handler' => $ttsHandler
|
||||||
|
]);
|
||||||
|
return $client->sendAndReceive();
|
||||||
|
}
|
||||||
|
}
|
46
vendor/composer/installed.json
vendored
46
vendor/composer/installed.json
vendored
@ -560,29 +560,18 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "guzzlehttp/guzzle",
|
"name": "guzzlehttp/guzzle",
|
||||||
"version": "7.7.0",
|
"version": "7.8.0",
|
||||||
"version_normalized": "7.7.0.0",
|
"version_normalized": "7.8.0.0",
|
||||||
"source": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "https://github.com/guzzle/guzzle.git",
|
|
||||||
"reference": "fb7566caccf22d74d1ab270de3551f72a58399f5"
|
|
||||||
},
|
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/guzzle/guzzle/zipball/fb7566caccf22d74d1ab270de3551f72a58399f5",
|
"url": "https://mirrors.tencent.com/repository/composer/guzzlehttp/guzzle/7.8.0/guzzlehttp-guzzle-7.8.0.zip",
|
||||||
"reference": "fb7566caccf22d74d1ab270de3551f72a58399f5",
|
"reference": "1110f66a6530a40fe7aea0378fe608ee2b2248f9",
|
||||||
"shasum": "",
|
"shasum": ""
|
||||||
"mirrors": [
|
|
||||||
{
|
|
||||||
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
|
|
||||||
"preferred": true
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"guzzlehttp/promises": "^1.5.3 || ^2.0",
|
"guzzlehttp/promises": "^1.5.3 || ^2.0.1",
|
||||||
"guzzlehttp/psr7": "^1.9.1 || ^2.4.5",
|
"guzzlehttp/psr7": "^1.9.1 || ^2.5.1",
|
||||||
"php": "^7.2.5 || ^8.0",
|
"php": "^7.2.5 || ^8.0",
|
||||||
"psr/http-client": "^1.0",
|
"psr/http-client": "^1.0",
|
||||||
"symfony/deprecation-contracts": "^2.2 || ^3.0"
|
"symfony/deprecation-contracts": "^2.2 || ^3.0"
|
||||||
@ -603,7 +592,7 @@
|
|||||||
"ext-intl": "Required for Internationalized Domain Name (IDN) support",
|
"ext-intl": "Required for Internationalized Domain Name (IDN) support",
|
||||||
"psr/log": "Required for using the Log middleware"
|
"psr/log": "Required for using the Log middleware"
|
||||||
},
|
},
|
||||||
"time": "2023-05-21T14:04:53+00:00",
|
"time": "2023-08-27T10:20:53+00:00",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"extra": {
|
"extra": {
|
||||||
"bamarni-bin": {
|
"bamarni-bin": {
|
||||||
@ -620,7 +609,6 @@
|
|||||||
"GuzzleHttp\\": "src/"
|
"GuzzleHttp\\": "src/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"notification-url": "https://packagist.org/downloads/",
|
|
||||||
"license": [
|
"license": [
|
||||||
"MIT"
|
"MIT"
|
||||||
],
|
],
|
||||||
@ -673,24 +661,6 @@
|
|||||||
"rest",
|
"rest",
|
||||||
"web service"
|
"web service"
|
||||||
],
|
],
|
||||||
"support": {
|
|
||||||
"issues": "https://github.com/guzzle/guzzle/issues",
|
|
||||||
"source": "https://github.com/guzzle/guzzle/tree/7.7.0"
|
|
||||||
},
|
|
||||||
"funding": [
|
|
||||||
{
|
|
||||||
"url": "https://github.com/GrahamCampbell",
|
|
||||||
"type": "github"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"url": "https://github.com/Nyholm",
|
|
||||||
"type": "github"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"url": "https://tidelift.com/funding/github/packagist/guzzlehttp/guzzle",
|
|
||||||
"type": "tidelift"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"install-path": "../guzzlehttp/guzzle"
|
"install-path": "../guzzlehttp/guzzle"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
10
vendor/composer/installed.php
vendored
10
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' => '6fd3d2a925259e9b1d493a43d011318e538a0a1b',
|
'reference' => 'c4b518e85efaed9caf7ac607e18217dfeb717f8b',
|
||||||
'type' => 'project',
|
'type' => 'project',
|
||||||
'install_path' => __DIR__ . '/../../',
|
'install_path' => __DIR__ . '/../../',
|
||||||
'aliases' => array(),
|
'aliases' => array(),
|
||||||
@ -83,9 +83,9 @@
|
|||||||
'dev_requirement' => false,
|
'dev_requirement' => false,
|
||||||
),
|
),
|
||||||
'guzzlehttp/guzzle' => array(
|
'guzzlehttp/guzzle' => array(
|
||||||
'pretty_version' => '7.7.0',
|
'pretty_version' => '7.8.0',
|
||||||
'version' => '7.7.0.0',
|
'version' => '7.8.0.0',
|
||||||
'reference' => 'fb7566caccf22d74d1ab270de3551f72a58399f5',
|
'reference' => '1110f66a6530a40fe7aea0378fe608ee2b2248f9',
|
||||||
'type' => 'library',
|
'type' => 'library',
|
||||||
'install_path' => __DIR__ . '/../guzzlehttp/guzzle',
|
'install_path' => __DIR__ . '/../guzzlehttp/guzzle',
|
||||||
'aliases' => array(),
|
'aliases' => array(),
|
||||||
@ -622,7 +622,7 @@
|
|||||||
'topthink/think' => array(
|
'topthink/think' => array(
|
||||||
'pretty_version' => 'dev-master',
|
'pretty_version' => 'dev-master',
|
||||||
'version' => 'dev-master',
|
'version' => 'dev-master',
|
||||||
'reference' => '6fd3d2a925259e9b1d493a43d011318e538a0a1b',
|
'reference' => 'c4b518e85efaed9caf7ac607e18217dfeb717f8b',
|
||||||
'type' => 'project',
|
'type' => 'project',
|
||||||
'install_path' => __DIR__ . '/../../',
|
'install_path' => __DIR__ . '/../../',
|
||||||
'aliases' => array(),
|
'aliases' => array(),
|
||||||
|
15
vendor/guzzlehttp/guzzle/CHANGELOG.md
vendored
15
vendor/guzzlehttp/guzzle/CHANGELOG.md
vendored
@ -3,6 +3,21 @@
|
|||||||
Please refer to [UPGRADING](UPGRADING.md) guide for upgrading to a major version.
|
Please refer to [UPGRADING](UPGRADING.md) guide for upgrading to a major version.
|
||||||
|
|
||||||
|
|
||||||
|
## 7.8.0 - 2023-08-27
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Support for PHP 8.3
|
||||||
|
- Added automatic closing of handles on `CurlFactory` object destruction
|
||||||
|
|
||||||
|
|
||||||
|
## 7.7.1 - 2023-08-27
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- Remove the need for `AllowDynamicProperties` in `CurlMultiHandler`
|
||||||
|
|
||||||
|
|
||||||
## 7.7.0 - 2023-05-21
|
## 7.7.0 - 2023-05-21
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
2
vendor/guzzlehttp/guzzle/UPGRADING.md
vendored
2
vendor/guzzlehttp/guzzle/UPGRADING.md
vendored
@ -27,7 +27,7 @@ Please make sure:
|
|||||||
- Function `GuzzleHttp\Exception\RequestException::getResponseBodySummary` is removed.
|
- Function `GuzzleHttp\Exception\RequestException::getResponseBodySummary` is removed.
|
||||||
Use `\GuzzleHttp\Psr7\get_message_body_summary` as an alternative.
|
Use `\GuzzleHttp\Psr7\get_message_body_summary` as an alternative.
|
||||||
- Function `GuzzleHttp\Cookie\CookieJar::getCookieValue` is removed.
|
- Function `GuzzleHttp\Cookie\CookieJar::getCookieValue` is removed.
|
||||||
- Request option `exception` is removed. Please use `http_errors`.
|
- Request option `exceptions` is removed. Please use `http_errors`.
|
||||||
- Request option `save_to` is removed. Please use `sink`.
|
- Request option `save_to` is removed. Please use `sink`.
|
||||||
- Pool option `pool_size` is removed. Please use `concurrency`.
|
- Pool option `pool_size` is removed. Please use `concurrency`.
|
||||||
- We now look for environment variables in the `$_SERVER` super global, due to thread safety issues with `getenv`. We continue to fallback to `getenv` in CLI environments, for maximum compatibility.
|
- We now look for environment variables in the `$_SERVER` super global, due to thread safety issues with `getenv`. We continue to fallback to `getenv` in CLI environments, for maximum compatibility.
|
||||||
|
4
vendor/guzzlehttp/guzzle/composer.json
vendored
4
vendor/guzzlehttp/guzzle/composer.json
vendored
@ -53,8 +53,8 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"php": "^7.2.5 || ^8.0",
|
"php": "^7.2.5 || ^8.0",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"guzzlehttp/promises": "^1.5.3 || ^2.0",
|
"guzzlehttp/promises": "^1.5.3 || ^2.0.1",
|
||||||
"guzzlehttp/psr7": "^1.9.1 || ^2.4.5",
|
"guzzlehttp/psr7": "^1.9.1 || ^2.5.1",
|
||||||
"psr/http-client": "^1.0",
|
"psr/http-client": "^1.0",
|
||||||
"symfony/deprecation-contracts": "^2.2 || ^3.0"
|
"symfony/deprecation-contracts": "^2.2 || ^3.0"
|
||||||
},
|
},
|
||||||
|
2
vendor/guzzlehttp/guzzle/src/Client.php
vendored
2
vendor/guzzlehttp/guzzle/src/Client.php
vendored
@ -202,7 +202,7 @@ class Client implements ClientInterface, \Psr\Http\Client\ClientInterface
|
|||||||
*
|
*
|
||||||
* @deprecated Client::getConfig will be removed in guzzlehttp/guzzle:8.0.
|
* @deprecated Client::getConfig will be removed in guzzlehttp/guzzle:8.0.
|
||||||
*/
|
*/
|
||||||
public function getConfig(?string $option = null)
|
public function getConfig(string $option = null)
|
||||||
{
|
{
|
||||||
return $option === null
|
return $option === null
|
||||||
? $this->config
|
? $this->config
|
||||||
|
@ -80,5 +80,5 @@ interface ClientInterface
|
|||||||
*
|
*
|
||||||
* @deprecated ClientInterface::getConfig will be removed in guzzlehttp/guzzle:8.0.
|
* @deprecated ClientInterface::getConfig will be removed in guzzlehttp/guzzle:8.0.
|
||||||
*/
|
*/
|
||||||
public function getConfig(?string $option = null);
|
public function getConfig(string $option = null);
|
||||||
}
|
}
|
||||||
|
@ -96,9 +96,6 @@ class CookieJar implements CookieJarInterface
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
public function toArray(): array
|
public function toArray(): array
|
||||||
{
|
{
|
||||||
return \array_map(static function (SetCookie $cookie): array {
|
return \array_map(static function (SetCookie $cookie): array {
|
||||||
@ -106,10 +103,7 @@ class CookieJar implements CookieJarInterface
|
|||||||
}, $this->getIterator()->getArrayCopy());
|
}, $this->getIterator()->getArrayCopy());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function clear(string $domain = null, string $path = null, string $name = null): void
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
public function clear(?string $domain = null, ?string $path = null, ?string $name = null): void
|
|
||||||
{
|
{
|
||||||
if (!$domain) {
|
if (!$domain) {
|
||||||
$this->cookies = [];
|
$this->cookies = [];
|
||||||
@ -126,25 +120,22 @@ class CookieJar implements CookieJarInterface
|
|||||||
$this->cookies = \array_filter(
|
$this->cookies = \array_filter(
|
||||||
$this->cookies,
|
$this->cookies,
|
||||||
static function (SetCookie $cookie) use ($path, $domain): bool {
|
static function (SetCookie $cookie) use ($path, $domain): bool {
|
||||||
return !($cookie->matchesPath($path) &&
|
return !($cookie->matchesPath($path)
|
||||||
$cookie->matchesDomain($domain));
|
&& $cookie->matchesDomain($domain));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
$this->cookies = \array_filter(
|
$this->cookies = \array_filter(
|
||||||
$this->cookies,
|
$this->cookies,
|
||||||
static function (SetCookie $cookie) use ($path, $domain, $name) {
|
static function (SetCookie $cookie) use ($path, $domain, $name) {
|
||||||
return !($cookie->getName() == $name &&
|
return !($cookie->getName() == $name
|
||||||
$cookie->matchesPath($path) &&
|
&& $cookie->matchesPath($path)
|
||||||
$cookie->matchesDomain($domain));
|
&& $cookie->matchesDomain($domain));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
public function clearSessionCookies(): void
|
public function clearSessionCookies(): void
|
||||||
{
|
{
|
||||||
$this->cookies = \array_filter(
|
$this->cookies = \array_filter(
|
||||||
@ -155,9 +146,6 @@ class CookieJar implements CookieJarInterface
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
public function setCookie(SetCookie $cookie): bool
|
public function setCookie(SetCookie $cookie): bool
|
||||||
{
|
{
|
||||||
// If the name string is empty (but not 0), ignore the set-cookie
|
// If the name string is empty (but not 0), ignore the set-cookie
|
||||||
@ -182,9 +170,9 @@ class CookieJar implements CookieJarInterface
|
|||||||
foreach ($this->cookies as $i => $c) {
|
foreach ($this->cookies as $i => $c) {
|
||||||
// Two cookies are identical, when their path, and domain are
|
// Two cookies are identical, when their path, and domain are
|
||||||
// identical.
|
// identical.
|
||||||
if ($c->getPath() != $cookie->getPath() ||
|
if ($c->getPath() != $cookie->getPath()
|
||||||
$c->getDomain() != $cookie->getDomain() ||
|
|| $c->getDomain() != $cookie->getDomain()
|
||||||
$c->getName() != $cookie->getName()
|
|| $c->getName() != $cookie->getName()
|
||||||
) {
|
) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -286,10 +274,10 @@ class CookieJar implements CookieJarInterface
|
|||||||
$path = $uri->getPath() ?: '/';
|
$path = $uri->getPath() ?: '/';
|
||||||
|
|
||||||
foreach ($this->cookies as $cookie) {
|
foreach ($this->cookies as $cookie) {
|
||||||
if ($cookie->matchesPath($path) &&
|
if ($cookie->matchesPath($path)
|
||||||
$cookie->matchesDomain($host) &&
|
&& $cookie->matchesDomain($host)
|
||||||
!$cookie->isExpired() &&
|
&& !$cookie->isExpired()
|
||||||
(!$cookie->getSecure() || $scheme === 'https')
|
&& (!$cookie->getSecure() || $scheme === 'https')
|
||||||
) {
|
) {
|
||||||
$values[] = $cookie->getName().'='
|
$values[] = $cookie->getName().'='
|
||||||
.$cookie->getValue();
|
.$cookie->getValue();
|
||||||
|
@ -14,6 +14,7 @@ use Psr\Http\Message\ResponseInterface;
|
|||||||
* cookies from a file, database, etc.
|
* cookies from a file, database, etc.
|
||||||
*
|
*
|
||||||
* @see https://docs.python.org/2/library/cookielib.html Inspiration
|
* @see https://docs.python.org/2/library/cookielib.html Inspiration
|
||||||
|
*
|
||||||
* @extends \IteratorAggregate<SetCookie>
|
* @extends \IteratorAggregate<SetCookie>
|
||||||
*/
|
*/
|
||||||
interface CookieJarInterface extends \Countable, \IteratorAggregate
|
interface CookieJarInterface extends \Countable, \IteratorAggregate
|
||||||
@ -61,7 +62,7 @@ interface CookieJarInterface extends \Countable, \IteratorAggregate
|
|||||||
* @param string|null $path Clears cookies matching a domain and path
|
* @param string|null $path Clears cookies matching a domain and path
|
||||||
* @param string|null $name Clears cookies matching a domain, path, and name
|
* @param string|null $name Clears cookies matching a domain, path, and name
|
||||||
*/
|
*/
|
||||||
public function clear(?string $domain = null, ?string $path = null, ?string $name = null): void;
|
public function clear(string $domain = null, string $path = null, string $name = null): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Discard all sessions cookies.
|
* Discard all sessions cookies.
|
||||||
|
@ -367,11 +367,11 @@ class CurlFactory implements CurlFactoryInterface
|
|||||||
// If it's a directory or a link to a directory use CURLOPT_CAPATH.
|
// If it's a directory or a link to a directory use CURLOPT_CAPATH.
|
||||||
// If not, it's probably a file, or a link to a file, so use CURLOPT_CAINFO.
|
// If not, it's probably a file, or a link to a file, so use CURLOPT_CAINFO.
|
||||||
if (
|
if (
|
||||||
\is_dir($options['verify']) ||
|
\is_dir($options['verify'])
|
||||||
(
|
|| (
|
||||||
\is_link($options['verify']) === true &&
|
\is_link($options['verify']) === true
|
||||||
($verifyLink = \readlink($options['verify'])) !== false &&
|
&& ($verifyLink = \readlink($options['verify'])) !== false
|
||||||
\is_dir($verifyLink)
|
&& \is_dir($verifyLink)
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
$conf[\CURLOPT_CAPATH] = $options['verify'];
|
$conf[\CURLOPT_CAPATH] = $options['verify'];
|
||||||
@ -627,4 +627,12 @@ class CurlFactory implements CurlFactoryInterface
|
|||||||
return \strlen($h);
|
return \strlen($h);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function __destruct()
|
||||||
|
{
|
||||||
|
foreach ($this->handles as $id => $handle) {
|
||||||
|
\curl_close($handle);
|
||||||
|
unset($this->handles[$id]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,11 +15,8 @@ use Psr\Http\Message\RequestInterface;
|
|||||||
* associative array of curl option constants mapping to values in the
|
* associative array of curl option constants mapping to values in the
|
||||||
* **curl** key of the provided request options.
|
* **curl** key of the provided request options.
|
||||||
*
|
*
|
||||||
* @property resource|\CurlMultiHandle $_mh Internal use only. Lazy loaded multi-handle.
|
|
||||||
*
|
|
||||||
* @final
|
* @final
|
||||||
*/
|
*/
|
||||||
#[\AllowDynamicProperties]
|
|
||||||
class CurlMultiHandler
|
class CurlMultiHandler
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -56,6 +53,9 @@ class CurlMultiHandler
|
|||||||
*/
|
*/
|
||||||
private $options = [];
|
private $options = [];
|
||||||
|
|
||||||
|
/** @var resource|\CurlMultiHandle */
|
||||||
|
private $_mh;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This handler accepts the following options:
|
* This handler accepts the following options:
|
||||||
*
|
*
|
||||||
@ -79,6 +79,10 @@ class CurlMultiHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->options = $options['options'] ?? [];
|
$this->options = $options['options'] ?? [];
|
||||||
|
|
||||||
|
// unsetting the property forces the first access to go through
|
||||||
|
// __get().
|
||||||
|
unset($this->_mh);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -44,7 +44,7 @@ class HandlerStack
|
|||||||
* handler is provided, the best handler for your
|
* handler is provided, the best handler for your
|
||||||
* system will be utilized.
|
* system will be utilized.
|
||||||
*/
|
*/
|
||||||
public static function create(?callable $handler = null): self
|
public static function create(callable $handler = null): self
|
||||||
{
|
{
|
||||||
$stack = new self($handler ?: Utils::chooseHandler());
|
$stack = new self($handler ?: Utils::chooseHandler());
|
||||||
$stack->push(Middleware::httpErrors(), 'http_errors');
|
$stack->push(Middleware::httpErrors(), 'http_errors');
|
||||||
@ -131,7 +131,7 @@ class HandlerStack
|
|||||||
* @param callable(callable): callable $middleware Middleware function
|
* @param callable(callable): callable $middleware Middleware function
|
||||||
* @param string $name Name to register for this middleware.
|
* @param string $name Name to register for this middleware.
|
||||||
*/
|
*/
|
||||||
public function unshift(callable $middleware, ?string $name = null): void
|
public function unshift(callable $middleware, string $name = null): void
|
||||||
{
|
{
|
||||||
\array_unshift($this->stack, [$middleware, $name]);
|
\array_unshift($this->stack, [$middleware, $name]);
|
||||||
$this->cached = null;
|
$this->cached = null;
|
||||||
|
@ -68,7 +68,7 @@ class MessageFormatter implements MessageFormatterInterface
|
|||||||
* @param ResponseInterface|null $response Response that was received
|
* @param ResponseInterface|null $response Response that was received
|
||||||
* @param \Throwable|null $error Exception that was received
|
* @param \Throwable|null $error Exception that was received
|
||||||
*/
|
*/
|
||||||
public function format(RequestInterface $request, ?ResponseInterface $response = null, ?\Throwable $error = null): string
|
public function format(RequestInterface $request, ResponseInterface $response = null, \Throwable $error = null): string
|
||||||
{
|
{
|
||||||
$cache = [];
|
$cache = [];
|
||||||
|
|
||||||
|
@ -14,5 +14,5 @@ interface MessageFormatterInterface
|
|||||||
* @param ResponseInterface|null $response Response that was received
|
* @param ResponseInterface|null $response Response that was received
|
||||||
* @param \Throwable|null $error Exception that was received
|
* @param \Throwable|null $error Exception that was received
|
||||||
*/
|
*/
|
||||||
public function format(RequestInterface $request, ?ResponseInterface $response = null, ?\Throwable $error = null): string;
|
public function format(RequestInterface $request, ResponseInterface $response = null, \Throwable $error = null): string;
|
||||||
}
|
}
|
||||||
|
@ -166,8 +166,8 @@ class RedirectMiddleware
|
|||||||
// not forcing RFC compliance, but rather emulating what all browsers
|
// not forcing RFC compliance, but rather emulating what all browsers
|
||||||
// would do.
|
// would do.
|
||||||
$statusCode = $response->getStatusCode();
|
$statusCode = $response->getStatusCode();
|
||||||
if ($statusCode == 303 ||
|
if ($statusCode == 303
|
||||||
($statusCode <= 302 && !$options['allow_redirects']['strict'])
|
|| ($statusCode <= 302 && !$options['allow_redirects']['strict'])
|
||||||
) {
|
) {
|
||||||
$safeMethods = ['GET', 'HEAD', 'OPTIONS'];
|
$safeMethods = ['GET', 'HEAD', 'OPTIONS'];
|
||||||
$requestMethod = $request->getMethod();
|
$requestMethod = $request->getMethod();
|
||||||
|
@ -46,8 +46,8 @@ final class TransferStats
|
|||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
RequestInterface $request,
|
RequestInterface $request,
|
||||||
?ResponseInterface $response = null,
|
ResponseInterface $response = null,
|
||||||
?float $transferTime = null,
|
float $transferTime = null,
|
||||||
$handlerErrorData = null,
|
$handlerErrorData = null,
|
||||||
array $handlerStats = []
|
array $handlerStats = []
|
||||||
) {
|
) {
|
||||||
|
2
vendor/services.php
vendored
2
vendor/services.php
vendored
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
// This file is automatically generated at:2023-09-04 18:38:31
|
// This file is automatically generated at:2023-09-12 10:55:16
|
||||||
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