|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class Base |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://www.icy2003.com/ |
|
6
|
|
|
* @author icy2003 <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2017, icy2003 |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace icy2003\php\iapis\baidu; |
|
10
|
|
|
|
|
11
|
|
|
use Exception; |
|
12
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
13
|
|
|
use icy2003\php\I; |
|
14
|
|
|
use icy2003\php\iapis\Api; |
|
15
|
|
|
use icy2003\php\ihelpers\Http; |
|
16
|
|
|
use icy2003\php\ihelpers\Json; |
|
17
|
|
|
use icy2003\php\ihelpers\Base64; |
|
18
|
|
|
use icy2003\php\icomponents\file\LocalFile; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* 百度 API 基类 |
|
22
|
|
|
*/ |
|
23
|
|
|
class Base extends Api |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* API KEY |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $_apiKey; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* secret KEY |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $_secretKey; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* 错误信息 |
|
41
|
|
|
* |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $_errorMap = []; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* 构造函数 |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $apiKey |
|
50
|
|
|
* @param string $secretKey |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct($apiKey, $secretKey) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->_apiKey = $apiKey; |
|
55
|
|
|
$this->_secretKey = $secretKey; |
|
56
|
|
|
$this->_errorMap = [ |
|
57
|
|
|
0 => '成功', |
|
58
|
|
|
1 => '服务器内部错误,请再次请求', |
|
59
|
|
|
2 => '服务暂不可用,请再次请求', |
|
60
|
|
|
3 => '调用的 API 不存在,请检查后重新尝试', |
|
61
|
|
|
4 => '集群超限额', |
|
62
|
|
|
6 => '无权限访问该用户数据', |
|
63
|
|
|
13 => '获取 token 失败', |
|
64
|
|
|
14 => 'IAM鉴权失败', |
|
65
|
|
|
15 => '应用不存在或者创建失败', |
|
66
|
|
|
17 => '每天请求量超限额', |
|
67
|
|
|
18 => 'QPS 超限额', |
|
68
|
|
|
19 => '请求总量超限额', |
|
69
|
|
|
100 => '包含了无效或错误参数,请检查代码', |
|
70
|
|
|
110 => 'Access Token 失效', |
|
71
|
|
|
111 => 'Access token 过期', |
|
72
|
|
|
216100 => '请求中包含非法参数,请检查后重新尝试', |
|
73
|
|
|
216101 => '缺少必须的参数,请检查参数是否有遗漏', |
|
74
|
|
|
216102 => '请求了不支持的服务,请检查调用的 url', |
|
75
|
|
|
216103 => '请求中某些参数过长,请检查后重新尝试', |
|
76
|
|
|
216110 => 'appid 不存在,请重新核对信息是否为后台应用列表中的 appid', |
|
77
|
|
|
216200 => '图片为空,请检查后重新尝试', |
|
78
|
|
|
216201 => '上传的图片格式错误,现阶段我们支持的图片格式为:PNG、JPG、JPEG、BMP,请进行转码或更换图片', |
|
79
|
|
|
216202 => '上传的图片大小错误,现阶段我们支持的图片大小为:base64编码后小于4M,分辨率不高于4096*4096,请重新上传图片', |
|
80
|
|
|
216203 => '上传的图片 base64 编码有误,请校验 base64 编码方式,并重新上传图片', |
|
81
|
|
|
216630 => '识别错误,请再次请求', |
|
82
|
|
|
216631 => '识别银行卡错误,出现此问题的原因一般为:您上传的图片非银行卡正面,上传了异形卡的图片或上传的银行卡正品图片不完整', |
|
83
|
|
|
216633 => '识别身份证错误,出现此问题的原因一般为:您上传了非身份证图片或您上传的身份证图片不完整', |
|
84
|
|
|
216634 => '检测错误,请再次请求', |
|
85
|
|
|
282000 => '服务器内部错误,请再次请求', |
|
86
|
|
|
282002 => '编码错误,请使用GBK编码', |
|
87
|
|
|
282003 => '请求参数缺失', |
|
88
|
|
|
282004 => '请求中包含非法参数,请检查后重新尝试', |
|
89
|
|
|
282005 => '处理批量任务时发生部分或全部错误,请根据具体错误码排查', |
|
90
|
|
|
282006 => '批量任务处理数量超出限制,请将任务数量减少到 10 或 10 以下', |
|
91
|
|
|
282008 => '仅支持 GBK 和 UTF-8,其余为不支持的字符编码,请检查后重新尝试', |
|
92
|
|
|
282011 => '未训练或未生效该接口', |
|
93
|
|
|
282100 => '图片压缩转码错误', |
|
94
|
|
|
282101 => '长图片切分数量超限', |
|
95
|
|
|
282102 => '未检测到图片中识别目标', |
|
96
|
|
|
282103 => '图片目标识别错误', |
|
97
|
|
|
282114 => 'URL 长度超过 1024 字节或为 0', |
|
98
|
|
|
282130 => '当前查询无结果返回,出现此问题的原因一般为:参数配置存在问题,请检查后重新尝试', |
|
99
|
|
|
282131 => '输入长度超限,请查看文档说明', |
|
100
|
|
|
282133 => '接口参数缺失', |
|
101
|
|
|
282134 => '输入为空', |
|
102
|
|
|
282300 => 'word 不在算法词典中', |
|
103
|
|
|
282301 => 'word_1 提交的词汇暂未收录,无法比对相似度', |
|
104
|
|
|
282302 => 'word_2 提交的词汇暂未收录,无法比对相似度', |
|
105
|
|
|
282303 => 'word_1和word_2暂未收录,无法比对相似度', |
|
106
|
|
|
282808 => 'request id 不存在', |
|
107
|
|
|
282809 => '返回结果请求错误(不属于 excel 或 json)', |
|
108
|
|
|
282810 => '图像识别错误', |
|
109
|
|
|
283300 => '入参格式有误,可检查下图片编码、代码格式是否有误', |
|
110
|
|
|
336000 => '服务器内部错误,请再次请求', |
|
111
|
|
|
336001 => '入参格式有误,比如缺少必要参数、图片base64编码错误等等,可检查下图片编码、代码格式是否有误', |
|
112
|
|
|
]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* access_token |
|
117
|
|
|
* |
|
118
|
|
|
* @var string |
|
119
|
|
|
*/ |
|
120
|
|
|
protected $_token; |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* 设置 access_token |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $token |
|
126
|
|
|
* |
|
127
|
|
|
* @return static |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setToken($token) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->_token = $token; |
|
132
|
|
|
|
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* 请求获得 access_token |
|
138
|
|
|
* |
|
139
|
|
|
* @return static |
|
140
|
|
|
*/ |
|
141
|
|
|
public function requestToken() |
|
142
|
|
|
{ |
|
143
|
|
|
if (null === $this->_token) { |
|
144
|
|
|
try { |
|
145
|
|
|
$this->_result = (array)Json::decode(Http::post('https://aip.baidubce.com/oauth/2.0/token', [], [ |
|
146
|
|
|
'grant_type' => 'client_credentials', |
|
147
|
|
|
'client_id' => $this->_apiKey, |
|
148
|
|
|
'client_secret' => $this->_secretKey, |
|
149
|
|
|
])); |
|
150
|
|
|
} catch (ClientException $e) { |
|
151
|
|
|
throw new Exception("access_token 获取失败,接口返回为:" . $e->getResponse()->getBody()->getContents()); |
|
152
|
|
|
} |
|
153
|
|
|
$this->_token = (string)$this->getResult(self::RESULT_TOKEN); |
|
154
|
|
|
} |
|
155
|
|
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* 是否成功 |
|
160
|
|
|
* |
|
161
|
|
|
* @return boolean |
|
162
|
|
|
*/ |
|
163
|
|
|
public function isSuccess() |
|
164
|
|
|
{ |
|
165
|
|
|
if (I::get($this->_result, 'error') || I::get($this->_result, 'error_code') > 0) { |
|
166
|
|
|
return false; |
|
167
|
|
|
} |
|
168
|
|
|
return true; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* 获取错误信息 |
|
173
|
|
|
* |
|
174
|
|
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getError() |
|
177
|
|
|
{ |
|
178
|
|
|
return (string)I::get($this->_errorMap, I::get($this->_result, 'error_code', 0), '未知错误'); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* access_token 键 |
|
183
|
|
|
*/ |
|
184
|
|
|
const RESULT_TOKEN = 'access_token'; |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* 加载一个图片 |
|
188
|
|
|
* |
|
189
|
|
|
* 可支持格式: |
|
190
|
|
|
* - base64:图像数据,大小不超过4M,最短边至少15px,最长边最大4096px,支持jjpg/jpeg/png/bmp格式 |
|
191
|
|
|
* - 文件 URL:图片完整URL,URL长度不超过1024字节,对应的 base64 数据限制如上,不支持https的图片链接 |
|
192
|
|
|
* |
|
193
|
|
|
* @param string $image |
|
194
|
|
|
* |
|
195
|
|
|
* @return static |
|
196
|
|
|
*/ |
|
197
|
|
|
public function image($image) |
|
198
|
|
|
{ |
|
199
|
|
|
if (Base64::isBase64($image)) { |
|
200
|
|
|
$this->_options['image'] = $image; |
|
201
|
|
|
} elseif ((new LocalFile())->isFile($image)) { |
|
202
|
|
|
$this->_options['image'] = Base64::fromFile($image); |
|
203
|
|
|
} else { |
|
204
|
|
|
throw new Exception('错误的图片类型'); |
|
205
|
|
|
} |
|
206
|
|
|
return $this; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* 加载一段文字 |
|
211
|
|
|
* |
|
212
|
|
|
* 可支持的格式: |
|
213
|
|
|
* - $text 为字符串:设置 text |
|
214
|
|
|
* - $text 为数组:设置 word_1 和 word_2 |
|
215
|
|
|
* |
|
216
|
|
|
* @param string|array $text |
|
217
|
|
|
* |
|
218
|
|
|
* @return static |
|
219
|
|
|
*/ |
|
220
|
|
|
public function text($text) |
|
221
|
|
|
{ |
|
222
|
|
|
if (is_string($text)) { |
|
223
|
|
|
$this->_options['text'] = $text; |
|
224
|
|
|
} elseif (is_array($text)) { |
|
|
|
|
|
|
225
|
|
|
$this->_options['word_1'] = I::get($text, 0); |
|
226
|
|
|
$this->_options['word_2'] = I::get($text, 1); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return $this; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|