|
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 icy2003\php\I; |
|
13
|
|
|
use icy2003\php\ihelpers\Http; |
|
14
|
|
|
use icy2003\php\ihelpers\Json; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* 百度 API 基类 |
|
18
|
|
|
*/ |
|
19
|
|
|
class Base |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* API KEY |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $_apiKey; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* secret KEY |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $_secretKey; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* 错误信息 |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $_errorMap = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* 构造函数 |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $apiKey |
|
46
|
|
|
* @param string $secretKey |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct($apiKey, $secretKey) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->_apiKey = $apiKey; |
|
51
|
|
|
$this->_secretKey = $secretKey; |
|
52
|
|
|
$this->_errorMap = [ |
|
53
|
|
|
'unknown client id' => 'API Key 不正确', |
|
54
|
|
|
'Client authentication failed' => 'Secret Key不正确', |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* API 返回原始数组 |
|
60
|
|
|
* |
|
61
|
|
|
* @var array |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $_result; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* access_token |
|
67
|
|
|
* |
|
68
|
|
|
* @var string |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $_token; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* 请求获得 access_token |
|
74
|
|
|
* |
|
75
|
|
|
* @return static |
|
76
|
|
|
*/ |
|
77
|
|
|
public function requestToken() |
|
78
|
|
|
{ |
|
79
|
|
|
if (null === $this->_token) { |
|
80
|
|
|
$this->_result = Json::decode(Http::post('https://aip.baidubce.com/oauth/2.0/token', [], [ |
|
81
|
|
|
'grant_type' => 'client_credentials', |
|
82
|
|
|
'client_id' => $this->_apiKey, |
|
83
|
|
|
'client_secret' => $this->_secretKey, |
|
84
|
|
|
])); |
|
85
|
|
|
$this->_token = $this->getResult(self::RESULT_TOKEN); |
|
86
|
|
|
if (null === $this->_token) { |
|
87
|
|
|
throw new Exception("access_token 获取失败"); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* 是否成功 |
|
95
|
|
|
* |
|
96
|
|
|
* @return boolean |
|
97
|
|
|
*/ |
|
98
|
|
|
public function isSuccess() |
|
99
|
|
|
{ |
|
100
|
|
|
if (I::get($this->_result, 'error')) { |
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* 获取错误信息 |
|
108
|
|
|
* |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getError() |
|
112
|
|
|
{ |
|
113
|
|
|
if ($error = I::get($this->_result, 'error_description')) { |
|
114
|
|
|
return I::get($this->_errorMap, $error); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return '未知错误'; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* access_token 键 |
|
122
|
|
|
*/ |
|
123
|
|
|
const RESULT_TOKEN = 'access_token'; |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* 获取结果 |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $key |
|
129
|
|
|
* |
|
130
|
|
|
* @return mixed |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getResult($key = null) |
|
133
|
|
|
{ |
|
134
|
|
|
if ($this->isSuccess()) { |
|
135
|
|
|
if (null === $key) { |
|
136
|
|
|
return $this->_result; |
|
137
|
|
|
} else { |
|
138
|
|
|
return I::get($this->_result, $key); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
throw new Exception($this->getError()); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* toArray 时调用的函数 |
|
146
|
|
|
* |
|
147
|
|
|
* @var callback |
|
148
|
|
|
*/ |
|
149
|
|
|
protected $_toArrayCall; |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* 智能返回有效数据 |
|
153
|
|
|
* |
|
154
|
|
|
* - 如果数据缺失,请使用 getResult() 获取原始数据 |
|
155
|
|
|
* |
|
156
|
|
|
* @return array |
|
157
|
|
|
*/ |
|
158
|
|
|
public function toArray() |
|
159
|
|
|
{ |
|
160
|
|
|
return I::trigger($this->_toArrayCall, [$this->getResult()]); |
|
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.