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

Commit

Permalink
Merge branch 'feature/handelParams' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
justmd5 committed Oct 18, 2019
2 parents 19fe97e + 5082935 commit ddf60e5
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 17 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
$config = [
'appKey' => '1106944xxx',
'appSecret' => 'dsgnbnWnX8Yxxxxxx',
'debug' => 0,//1 show debug info
'debug' => true,//true show debug info
];
$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'=>'腾讯人工智能',
Expand All @@ -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)
### 帮助
Expand All @@ -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 | <div>非全部接口实现</div>| https://github.com/w89612b/qqai-api-sdk |
| 1109527533 | Python | <div>非全部接口实现(完善中)</div>|https://gitee.com/french-home/TencentAISDK |
| 1928881525 | .NET(C#) | <div>OCR接口实现人脸模块接口实现</div>|https://gitee.com/ch_1928881525/Tentcent.Ai |
| 910139966 | PHP | ALL | https://github.com/justmd5/tencent-ai|

## License

Expand Down
17 changes: 10 additions & 7 deletions src/Core/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@
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/';

/**
* @var string
*/
protected $category;
/**
* @var \Justmd5\TencentAi\Core\Signature
* @var Signature
*/
protected $signature;
protected $classify;
Expand All @@ -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 = [])
{
Expand All @@ -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'])) {
Expand Down
1 change: 1 addition & 0 deletions src/Core/ApplicationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Justmd5\TencentAi\Core;

use Justmd5\TencentAi\Core\Traits\FilterTrait;
use Pimple\Container;
use Pimple\ServiceProviderInterface;

Expand Down
6 changes: 0 additions & 6 deletions src/Core/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions src/Core/Traits/ArgumentProcessingTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Justmd5\TencentAi\Core\Traits;

use Justmd5\TencentAi\Core\Signature;

trait ArgumentProcessingTrait
{
public function processParams(Signature $signature, $params)
{
if (empty($params['nonce_str'])) {
$params['nonce_str'] = md5(uniqid("TencentAi_"));
}
if (empty($params['time_stamp'])) {
$params['time_stamp'] = strval(time());
}
if (isset($params['image']) && is_file($params['image'])) {
$params['image'] = base64_encode(file_get_contents($params['image']));
}
$params['sign'] = $signature->getReqSign($params);

return $params;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Time: 下午7:53.
*/

namespace Justmd5\TencentAi\Core;
namespace Justmd5\TencentAi\Core\Traits;

trait FilterTrait
{
Expand Down

0 comments on commit ddf60e5

Please sign in to comment.