Skip to content
This repository was archived by the owner on Jun 29, 2022. It is now read-only.

Commit ddf60e5

Browse files
author
justmd5
committed
Merge branch 'feature/handelParams' into develop
2 parents 19fe97e + 5082935 commit ddf60e5

File tree

6 files changed

+55
-17
lines changed

6 files changed

+55
-17
lines changed

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@
3838
$config = [
3939
'appKey' => '1106944xxx',
4040
'appSecret' => 'dsgnbnWnX8Yxxxxxx',
41-
'debug' => 0,//1 show debug info
41+
'debug' => true,//true show debug info
4242
];
4343
$AI = new \Justmd5\TencentAi\Ai($config);
4444

4545
```
4646

4747
### 接口调用示例
4848
> [智能闲聊](https://ai.qq.com/doc/nlpchat.shtml) url: https://api.ai.qq.com/fcgi-bin/nlp/nlp_textchat
49-
> 请求示例如下
49+
> 请求示例1
5050
```
5151
$params = [
5252
'question'=>'腾讯人工智能',
@@ -58,6 +58,22 @@ try {
5858
dd($e);
5959
}
6060
```
61+
> 请求示例2:
62+
```
63+
$params = [
64+
//image 支持两种传递参数方式
65+
// 'image' => base64_encode(file_get_contents(__DIR__ . '/1571126902_843200.jpg')),//old
66+
'image' => __DIR__ . '/1571126902_843200.jpg',//new
67+
'session_id' => time(),
68+
];
69+
try {
70+
var_dump($AI->vision->request('imgtotext', $params));
71+
} catch (\Justmd5\TencentAi\Exception\NotFoundException $e) {
72+
print_r($e->getMessage());
73+
} catch (\Justmd5\TencentAi\Exception\IllegalParameterException $e) {
74+
print_r($e->getMessage());
75+
}
76+
```
6177
### 文档
6278
[Tencent AI](https://ai.qq.com) · [Official Documents](https://ai.qq.com/doc/index.shtml)
6379
### 帮助
@@ -78,12 +94,12 @@ qq群
7894

7995
| SDK 联系人 QQ | 语言 | 实现接口 | 源代码&SDK 地址 |
8096
| --- | --- | --- | --- |
81-
| 910139966 | PHP | ALL | https://github.com/justmd5/tencent-ai|
8297
| 783021975 | JAVA | ALL | https://gitee.com/xshuai/taip|
8398
| 1361653339 | Golang | ALL | https://github.com/shiguanghuxian/txai |
8499
| 1280827369 | NodeJS | <div>非全部接口实现</div>| https://github.com/w89612b/qqai-api-sdk |
85100
| 1109527533 | Python | <div>非全部接口实现(完善中)</div>|https://gitee.com/french-home/TencentAISDK |
86101
| 1928881525 | .NET(C#) | <div>OCR接口实现人脸模块接口实现</div>|https://gitee.com/ch_1928881525/Tentcent.Ai |
102+
| 910139966 | PHP | ALL | https://github.com/justmd5/tencent-ai|
87103

88104
## License
89105

src/Core/API.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@
1313
use Justmd5\TencentAi\Exception\NotFoundException;
1414
use Overtrue\Validation\Factory as ValidatorFactory;
1515
use Overtrue\Validation\Translator;
16+
use Justmd5\TencentAi\Core\Traits\ArgumentProcessingTrait;
1617

1718
class API extends AbstractAPI
1819
{
20+
use ArgumentProcessingTrait;
21+
1922
const BASE_API = 'https://api.ai.qq.com/fcgi-bin/';
2023

2124
/**
2225
* @var string
2326
*/
2427
protected $category;
2528
/**
26-
* @var \Justmd5\TencentAi\Core\Signature
29+
* @var Signature
2730
*/
2831
protected $signature;
2932
protected $classify;
@@ -40,13 +43,13 @@ public function __construct(Signature $signature, $classify, $filter)
4043
* 请求API.
4144
*
4245
* @param string $method
43-
* @param array $params
44-
* @param array $files
45-
*
46-
* @throws \Justmd5\TencentAi\Exception\NotFoundException
47-
* @throws \Justmd5\TencentAi\Exception\IllegalParameterException
46+
* @param array $params
47+
* @param array $files
4848
*
4949
* @return array
50+
* @throws IllegalParameterException
51+
*
52+
* @throws NotFoundException
5053
*/
5154
public function request($method, $params = [], $files = [])
5255
{
@@ -60,7 +63,7 @@ public function request($method, $params = [], $files = [])
6063
throw new IllegalParameterException(sprintf('参数错误:[%s]', json_encode($validator->errors(), JSON_UNESCAPED_UNICODE)));
6164
}
6265
$http = $this->getHttp();
63-
$params['sign'] = $this->signature->getReqSign($params);
66+
$params=$this->processParams($this->signature,$params);
6467
$response = $files ? $http->upload($url, $params, $files) : $http->post($url, $params);
6568
$result = json_decode(strval($response->getBody()), true);
6669
if (isset($result['ret'])) {

src/Core/ApplicationProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Justmd5\TencentAi\Core;
1010

11+
use Justmd5\TencentAi\Core\Traits\FilterTrait;
1112
use Pimple\Container;
1213
use Pimple\ServiceProviderInterface;
1314

src/Core/Signature.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ public function __construct($appId, $secret)
3939
public function getReqSign(&$params)
4040
{
4141
$params['app_id'] = $this->appId;
42-
if (empty($params['nonce_str'])) {
43-
$params['nonce_str'] = md5(uniqid("{$params['app_id']}_"));
44-
}
45-
if (empty($params['time_stamp'])) {
46-
$params['time_stamp'] = strval(time());
47-
}
4842
ksort($params);
4943
$str = '';
5044
array_walk($params, function ($item, $key) use (&$str) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Justmd5\TencentAi\Core\Traits;
4+
5+
use Justmd5\TencentAi\Core\Signature;
6+
7+
trait ArgumentProcessingTrait
8+
{
9+
public function processParams(Signature $signature, $params)
10+
{
11+
if (empty($params['nonce_str'])) {
12+
$params['nonce_str'] = md5(uniqid("TencentAi_"));
13+
}
14+
if (empty($params['time_stamp'])) {
15+
$params['time_stamp'] = strval(time());
16+
}
17+
if (isset($params['image']) && is_file($params['image'])) {
18+
$params['image'] = base64_encode(file_get_contents($params['image']));
19+
}
20+
$params['sign'] = $signature->getReqSign($params);
21+
22+
return $params;
23+
}
24+
}

src/Core/FilterTrait.php renamed to src/Core/Traits/FilterTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Time: 下午7:53.
77
*/
88

9-
namespace Justmd5\TencentAi\Core;
9+
namespace Justmd5\TencentAi\Core\Traits;
1010

1111
trait FilterTrait
1212
{

0 commit comments

Comments
 (0)