From 1d1d01032b0f0a417748218ef9cc59191cd703c5 Mon Sep 17 00:00:00 2001 From: justmd5 Date: Fri, 18 Oct 2019 15:37:32 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A4=84=E7=90=86=E5=8F=82=E6=95=B0?= =?UTF-8?q?=EF=BC=8Cimage=20=E5=8F=82=E6=95=B0=E6=94=AF=E6=8C=81base=5F64?= =?UTF-8?q?=E5=92=8C=E5=9B=BE=E7=89=87=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/Core/API.php | 17 +++++++++------ src/Core/ApplicationProvider.php | 1 + src/Core/Signature.php | 6 ------ src/Core/Traits/ArgumentProcessingTrait.php | 24 +++++++++++++++++++++ src/Core/{ => Traits}/FilterTrait.php | 2 +- 6 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 src/Core/Traits/ArgumentProcessingTrait.php rename src/Core/{ => Traits}/FilterTrait.php (99%) diff --git a/README.md b/README.md index dc56cd9..7736416 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ $config = [ 'appKey' => '1106944xxx', 'appSecret' => 'dsgnbnWnX8Yxxxxxx', - 'debug' => 0,//1 show debug info + 'debug' => true,//true show debug info ]; $AI = new \Justmd5\TencentAi\Ai($config); diff --git a/src/Core/API.php b/src/Core/API.php index 107e3a0..8f8953a 100644 --- a/src/Core/API.php +++ b/src/Core/API.php @@ -13,9 +13,12 @@ use Justmd5\TencentAi\Exception\NotFoundException; use Overtrue\Validation\Factory as ValidatorFactory; use Overtrue\Validation\Translator; +use Justmd5\TencentAi\Core\Traits\ArgumentProcessingTrait; class API extends AbstractAPI { + use ArgumentProcessingTrait; + const BASE_API = 'https://api.ai.qq.com/fcgi-bin/'; /** @@ -23,7 +26,7 @@ class API extends AbstractAPI */ protected $category; /** - * @var \Justmd5\TencentAi\Core\Signature + * @var Signature */ protected $signature; protected $classify; @@ -40,13 +43,13 @@ public function __construct(Signature $signature, $classify, $filter) * 请求API. * * @param string $method - * @param array $params - * @param array $files - * - * @throws \Justmd5\TencentAi\Exception\NotFoundException - * @throws \Justmd5\TencentAi\Exception\IllegalParameterException + * @param array $params + * @param array $files * * @return array + * @throws IllegalParameterException + * + * @throws NotFoundException */ public function request($method, $params = [], $files = []) { @@ -60,7 +63,7 @@ public function request($method, $params = [], $files = []) throw new IllegalParameterException(sprintf('参数错误:[%s]', json_encode($validator->errors(), JSON_UNESCAPED_UNICODE))); } $http = $this->getHttp(); - $params['sign'] = $this->signature->getReqSign($params); + $params=$this->processParams($this->signature,$params); $response = $files ? $http->upload($url, $params, $files) : $http->post($url, $params); $result = json_decode(strval($response->getBody()), true); if (isset($result['ret'])) { diff --git a/src/Core/ApplicationProvider.php b/src/Core/ApplicationProvider.php index 76838e4..0bba804 100644 --- a/src/Core/ApplicationProvider.php +++ b/src/Core/ApplicationProvider.php @@ -8,6 +8,7 @@ namespace Justmd5\TencentAi\Core; +use Justmd5\TencentAi\Core\Traits\FilterTrait; use Pimple\Container; use Pimple\ServiceProviderInterface; diff --git a/src/Core/Signature.php b/src/Core/Signature.php index 8e2c605..4fcbe51 100644 --- a/src/Core/Signature.php +++ b/src/Core/Signature.php @@ -39,12 +39,6 @@ public function __construct($appId, $secret) public function getReqSign(&$params) { $params['app_id'] = $this->appId; - if (empty($params['nonce_str'])) { - $params['nonce_str'] = md5(uniqid("{$params['app_id']}_")); - } - if (empty($params['time_stamp'])) { - $params['time_stamp'] = strval(time()); - } ksort($params); $str = ''; array_walk($params, function ($item, $key) use (&$str) { diff --git a/src/Core/Traits/ArgumentProcessingTrait.php b/src/Core/Traits/ArgumentProcessingTrait.php new file mode 100644 index 0000000..0c63bae --- /dev/null +++ b/src/Core/Traits/ArgumentProcessingTrait.php @@ -0,0 +1,24 @@ +getReqSign($params); + + return $params; + } +} \ No newline at end of file diff --git a/src/Core/FilterTrait.php b/src/Core/Traits/FilterTrait.php similarity index 99% rename from src/Core/FilterTrait.php rename to src/Core/Traits/FilterTrait.php index 03eb8e9..27ab5aa 100644 --- a/src/Core/FilterTrait.php +++ b/src/Core/Traits/FilterTrait.php @@ -6,7 +6,7 @@ * Time: 下午7:53. */ -namespace Justmd5\TencentAi\Core; +namespace Justmd5\TencentAi\Core\Traits; trait FilterTrait { From 50829356a81540dffc823a6d704e95d3a2e6ed49 Mon Sep 17 00:00:00 2001 From: justmd5 Date: Fri, 18 Oct 2019 15:43:21 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E4=B8=AD=E5=90=AB=E5=9B=BE=E7=89=87=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7736416..45ae57e 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ $AI = new \Justmd5\TencentAi\Ai($config); ### 接口调用示例 > [智能闲聊](https://ai.qq.com/doc/nlpchat.shtml) url: https://api.ai.qq.com/fcgi-bin/nlp/nlp_textchat -> 请求示例如下: +> 请求示例1: ``` $params = [ 'question'=>'腾讯人工智能', @@ -58,6 +58,22 @@ try { dd($e); } ``` +> 请求示例2: +``` +$params = [ +//image 支持两种传递参数方式 +// 'image' => base64_encode(file_get_contents(__DIR__ . '/1571126902_843200.jpg')),//old + 'image' => __DIR__ . '/1571126902_843200.jpg',//new + 'session_id' => time(), +]; +try { + var_dump($AI->vision->request('imgtotext', $params)); +} catch (\Justmd5\TencentAi\Exception\NotFoundException $e) { + print_r($e->getMessage()); +} catch (\Justmd5\TencentAi\Exception\IllegalParameterException $e) { + print_r($e->getMessage()); +} +``` ### 文档 [Tencent AI](https://ai.qq.com) · [Official Documents](https://ai.qq.com/doc/index.shtml) ### 帮助 @@ -78,12 +94,12 @@ qq群 | SDK 联系人 QQ | 语言 | 实现接口 | 源代码&SDK 地址 | | --- | --- | --- | --- | -| 910139966 | PHP | ALL | https://github.com/justmd5/tencent-ai| | 783021975 | JAVA | ALL | https://gitee.com/xshuai/taip| | 1361653339 | Golang | ALL | https://github.com/shiguanghuxian/txai | | 1280827369 | NodeJS |
非全部接口实现
| https://github.com/w89612b/qqai-api-sdk | | 1109527533 | Python |
非全部接口实现(完善中)
|https://gitee.com/french-home/TencentAISDK | | 1928881525 | .NET(C#) |
OCR接口实现人脸模块接口实现
|https://gitee.com/ch_1928881525/Tentcent.Ai | +| 910139966 | PHP | ALL | https://github.com/justmd5/tencent-ai| ## License