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\ihelpers\Http; |
15
|
|
|
use icy2003\php\ihelpers\Json; |
16
|
|
|
use icy2003\php\ihelpers\Base64; |
17
|
|
|
use icy2003\php\icomponents\file\LocalFile; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* 百度 API 基类 |
21
|
|
|
*/ |
22
|
|
|
class Base |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* API KEY |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $_apiKey; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* secret KEY |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $_secretKey; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* 错误信息 |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $_errorMap = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* 构造函数 |
47
|
|
|
* |
48
|
|
|
* @param string $apiKey |
49
|
|
|
* @param string $secretKey |
50
|
|
|
*/ |
51
|
|
|
public function __construct($apiKey, $secretKey) |
52
|
|
|
{ |
53
|
|
|
$this->_apiKey = $apiKey; |
54
|
|
|
$this->_secretKey = $secretKey; |
55
|
|
|
$this->_errorMap = [ |
56
|
|
|
0 => '成功', |
57
|
|
|
1 => '服务器内部错误,请再次请求', |
58
|
|
|
2 => '服务暂不可用,请再次请求', |
59
|
|
|
3 => '调用的 API 不存在,请检查后重新尝试', |
60
|
|
|
4 => '集群超限额', |
61
|
|
|
6 => '无权限访问该用户数据', |
62
|
|
|
13 => '获取 token 失败', |
63
|
|
|
14 => 'IAM鉴权失败', |
64
|
|
|
15 => '应用不存在或者创建失败', |
65
|
|
|
17 => '每天请求量超限额', |
66
|
|
|
18 => 'QPS 超限额', |
67
|
|
|
19 => '请求总量超限额', |
68
|
|
|
100 => '包含了无效或错误参数,请检查代码', |
69
|
|
|
110 => 'Access Token 失效', |
70
|
|
|
111 => 'Access token 过期', |
71
|
|
|
216100 => '请求中包含非法参数,请检查后重新尝试', |
72
|
|
|
216101 => '缺少必须的参数,请检查参数是否有遗漏', |
73
|
|
|
216102 => '请求了不支持的服务,请检查调用的 url', |
74
|
|
|
216103 => '请求中某些参数过长,请检查后重新尝试', |
75
|
|
|
216110 => 'appid 不存在,请重新核对信息是否为后台应用列表中的 appid', |
76
|
|
|
216200 => '图片为空,请检查后重新尝试', |
77
|
|
|
216201 => '上传的图片格式错误,现阶段我们支持的图片格式为:PNG、JPG、JPEG、BMP,请进行转码或更换图片', |
78
|
|
|
216202 => '上传的图片大小错误,现阶段我们支持的图片大小为:base64编码后小于4M,分辨率不高于4096*4096,请重新上传图片', |
79
|
|
|
216203 => '上传的图片 base64 编码有误,请校验 base64 编码方式,并重新上传图片', |
80
|
|
|
216630 => '识别错误,请再次请求', |
81
|
|
|
216631 => '识别银行卡错误,出现此问题的原因一般为:您上传的图片非银行卡正面,上传了异形卡的图片或上传的银行卡正品图片不完整', |
82
|
|
|
216633 => '识别身份证错误,出现此问题的原因一般为:您上传了非身份证图片或您上传的身份证图片不完整', |
83
|
|
|
216634 => '检测错误,请再次请求', |
84
|
|
|
282000 => '服务器内部错误,请再次请求', |
85
|
|
|
282002 => '编码错误,请使用GBK编码', |
86
|
|
|
282003 => '请求参数缺失', |
87
|
|
|
282004 => '请求中包含非法参数,请检查后重新尝试', |
88
|
|
|
282005 => '处理批量任务时发生部分或全部错误,请根据具体错误码排查', |
89
|
|
|
282006 => '批量任务处理数量超出限制,请将任务数量减少到 10 或 10 以下', |
90
|
|
|
282008 => '仅支持 GBK 和 UTF-8,其余为不支持的字符编码,请检查后重新尝试', |
91
|
|
|
282011 => '未训练或未生效该接口', |
92
|
|
|
282100 => '图片压缩转码错误', |
93
|
|
|
282101 => '长图片切分数量超限', |
94
|
|
|
282102 => '未检测到图片中识别目标', |
95
|
|
|
282103 => '图片目标识别错误', |
96
|
|
|
282114 => 'URL 长度超过 1024 字节或为 0', |
97
|
|
|
282130 => '当前查询无结果返回,出现此问题的原因一般为:参数配置存在问题,请检查后重新尝试', |
98
|
|
|
282131 => '输入长度超限,请查看文档说明', |
99
|
|
|
282133 => '接口参数缺失', |
100
|
|
|
282134 => '输入为空', |
101
|
|
|
282300 => 'word 不在算法词典中', |
102
|
|
|
282301 => 'word_1 提交的词汇暂未收录,无法比对相似度', |
103
|
|
|
282302 => 'word_2 提交的词汇暂未收录,无法比对相似度', |
104
|
|
|
282303 => 'word_1和word_2暂未收录,无法比对相似度', |
105
|
|
|
282808 => 'request id 不存在', |
106
|
|
|
282809 => '返回结果请求错误(不属于 excel 或 json)', |
107
|
|
|
282810 => '图像识别错误', |
108
|
|
|
283300 => '入参格式有误,可检查下图片编码、代码格式是否有误', |
109
|
|
|
336000 => '服务器内部错误,请再次请求', |
110
|
|
|
336001 => '入参格式有误,比如缺少必要参数、图片base64编码错误等等,可检查下图片编码、代码格式是否有误', |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* API 返回原始数组 |
116
|
|
|
* |
117
|
|
|
* @var array |
118
|
|
|
*/ |
119
|
|
|
protected $_result; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* access_token |
123
|
|
|
* |
124
|
|
|
* @var string |
125
|
|
|
*/ |
126
|
|
|
protected $_token; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* 设置 access_token |
130
|
|
|
* |
131
|
|
|
* @param string $token |
132
|
|
|
* |
133
|
|
|
* @return static |
134
|
|
|
*/ |
135
|
|
|
public function setToken($token) |
136
|
|
|
{ |
137
|
|
|
$this->_token = $token; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* 请求获得 access_token |
144
|
|
|
* |
145
|
|
|
* @return static |
146
|
|
|
*/ |
147
|
|
|
public function requestToken() |
148
|
|
|
{ |
149
|
|
|
if (null === $this->_token) { |
150
|
|
|
try { |
151
|
|
|
$this->_result = Json::decode(Http::post('https://aip.baidubce.com/oauth/2.0/token', [], [ |
152
|
|
|
'grant_type' => 'client_credentials', |
153
|
|
|
'client_id' => $this->_apiKey, |
154
|
|
|
'client_secret' => $this->_secretKey, |
155
|
|
|
])); |
156
|
|
|
} catch (ClientException $e) { |
157
|
|
|
throw new Exception("access_token 获取失败,接口返回为:" . $e->getResponse()->getBody()->getContents()); |
158
|
|
|
} |
159
|
|
|
$this->_token = $this->getResult(self::RESULT_TOKEN); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* 是否成功 |
166
|
|
|
* |
167
|
|
|
* @return boolean |
168
|
|
|
*/ |
169
|
|
|
public function isSuccess() |
170
|
|
|
{ |
171
|
|
|
if (I::get($this->_result, 'error') || I::get($this->_result, 'error_code') > 0) { |
172
|
|
|
return false; |
173
|
|
|
} |
174
|
|
|
return true; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* 获取错误信息 |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function getError() |
183
|
|
|
{ |
184
|
|
|
return I::get($this->_errorMap, I::get($this->_result, 'error_code', 0), '未知错误'); |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* access_token 键 |
189
|
|
|
*/ |
190
|
|
|
const RESULT_TOKEN = 'access_token'; |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* 获取结果 |
194
|
|
|
* |
195
|
|
|
* @param string $key |
196
|
|
|
* |
197
|
|
|
* @return mixed |
198
|
|
|
*/ |
199
|
|
|
public function getResult($key = null) |
200
|
|
|
{ |
201
|
|
|
if ($this->isSuccess()) { |
202
|
|
|
if (null === $key) { |
203
|
|
|
return $this->_result; |
204
|
|
|
} else { |
205
|
|
|
return I::get($this->_result, $key); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
throw new Exception($this->getError()); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* toArray 时调用的函数 |
213
|
|
|
* |
214
|
|
|
* @var callback |
215
|
|
|
*/ |
216
|
|
|
protected $_toArrayCall; |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* 智能返回有效数据 |
220
|
|
|
* |
221
|
|
|
* - 如果数据缺失,请使用 getResult() 获取原始数据 |
222
|
|
|
* |
223
|
|
|
* @return array |
224
|
|
|
*/ |
225
|
|
|
public function toArray() |
226
|
|
|
{ |
227
|
|
|
return I::call($this->_toArrayCall, [$this->getResult()]); |
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* 选项列表 |
232
|
|
|
* |
233
|
|
|
* @var array |
234
|
|
|
*/ |
235
|
|
|
protected $_options = []; |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* 设置选项 |
239
|
|
|
* |
240
|
|
|
* @param array $options |
241
|
|
|
* |
242
|
|
|
* @return static |
243
|
|
|
*/ |
244
|
|
|
public function setOptions($options) |
245
|
|
|
{ |
246
|
|
|
$this->_options = Arrays::merge($this->_options, $options); |
|
|
|
|
247
|
|
|
return $this; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* toString 魔术方法 |
252
|
|
|
* |
253
|
|
|
* @return string |
254
|
|
|
*/ |
255
|
|
|
public function __toString() |
256
|
|
|
{ |
257
|
|
|
return Json::encode($this->_result); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* 加载一个图片 |
262
|
|
|
* |
263
|
|
|
* 可支持格式: |
264
|
|
|
* - base64:图像数据,大小不超过4M,最短边至少15px,最长边最大4096px,支持jjpg/jpeg/png/bmp格式 |
265
|
|
|
* - 文件 URL:图片完整URL,URL长度不超过1024字节,对应的 base64 数据限制如上,不支持https的图片链接 |
266
|
|
|
* |
267
|
|
|
* @param string $image |
268
|
|
|
* |
269
|
|
|
* @return static |
270
|
|
|
*/ |
271
|
|
|
public function image($image) |
272
|
|
|
{ |
273
|
|
|
if (Base64::isBase64($image)) { |
274
|
|
|
$this->_options['image'] = $image; |
275
|
|
|
} elseif ((new LocalFile())->isFile($image)) { |
276
|
|
|
$this->_options['image'] = Base64::fromFile($image); |
277
|
|
|
} else { |
278
|
|
|
throw new Exception('错误的图片类型'); |
279
|
|
|
} |
280
|
|
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* 加载一段文字 |
285
|
|
|
* |
286
|
|
|
* 可支持的格式: |
287
|
|
|
* - $text 为字符串:设置 text |
288
|
|
|
* - $text 为数组:设置 word_1 和 word_2 |
289
|
|
|
* |
290
|
|
|
* @param string|array $text |
291
|
|
|
* |
292
|
|
|
* @return static |
293
|
|
|
*/ |
294
|
|
|
public function text($text) |
295
|
|
|
{ |
296
|
|
|
if (is_string($text)) { |
297
|
|
|
$this->_options['text'] = $text; |
298
|
|
|
} elseif (is_array($text)) { |
|
|
|
|
299
|
|
|
$this->_options['word_1'] = I::get($text, 0); |
300
|
|
|
$this->_options['word_2'] = I::get($text, 1); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
return $this; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.