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