forked from crossmaya/qiniu-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdk.php
230 lines (207 loc) · 4.81 KB
/
sdk.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
class SDK implements ArrayAccess {
const QINIU_UP_HOST = 'http://up.qiniu.com';
const QINIU_RS_HOST = 'http://rs.qbox.me';
const QINIU_RSF_HOST= 'http://rsf.qbox.me';
//查看
//删除
//复制 x
//移动 x
//上传 x
protected $access_token ;
protected $secret_token ;
protected $bucket;
protected $cache = array();
protected $aliases = array(); //文件别名, 针对文件名比较长的文件
//curl
protected $ch;
protected $headers;
protected $options = array();
protected $response;
protected $info;
protected $errno;
protected $error;
public function __construct($access_token, $secret_token, $bucket = null)
{
$this->access_token = $access_token;
$this->secret_token = $secret_token;
$this->bucket = $bucket;
}
public function getBucket()
{
return $this->bucket;
}
public function setBucket($bucket)
{
$this->bucket = $bucket;
}
/**
* 查看指定文件信息。
* @param string $key 文件名或者目录+文件名
* @return Array|boolean 成功返回文件内容,否会返回false.
*/
public function stat($key)
{
list($bucket, $key) = $this->parseKey($key);
if ( is_null($bucket) )
{
die('error');
}
$url = self::QINIU_RS_HOST .'/stat/' . $this->encode("$bucket:$key");
return $this->get($url);
}
public function delete($key)
{
list($bucket, $key) = $this->parseKey($key);
if ( is_null($bucket) )
{
die('error');
}
$url = self::QINIU_RS_HOST .'/delete/' . $this->encode("$bucket:$key");
return $this->get($url);
}
protected function parseKey($key)
{
$key = $this->getAlias($key);
if ( isset($this->cache[$key]) )
{
return $this->cache[$key];
}
$segments = explode("|", $key);
if ( count($segments) === 1 )
{
$this->cache[$key] = array($this->bucket, $segments[0]);
}
else
{
$temp = implode('|', array_slice($segments, 1));
$this->cache[$key] = array($segments[0], $temp);
}
return $this->cache[$key];
}
public function getAlias($key)
{
return isset($this->aliases[$key]) ? $this->aliases[$key] : $key;
}
public function accessToken($url, $body = false)
{
$url = parse_url($url);
$result = '';
if (isset($url['path'])) {
$result = $url['path'];
}
if (isset($url['query'])) {
$result .= '?' . $url['query'];
}
$result .= "\n";
if ($body) {
$result .= $body;
}
$sign = hash_hmac('sha1', $result, $this->secret_token, true);
return $this->access_token . ':' . $this->encode($sign);
}
public function get($url, $options = array())
{
$this->ch = curl_init();
$token = $this->accessToken($url);
$options[CURLOPT_HTTPHEADER] = array('Authorization: QBox '. $token);
$this->options[CURLOPT_URL] = $url;
$this->options = $options + $this->options;
//临时处理逻辑
return $this->execute();
}
protected function execute()
{
if ( !$this->option(CURLOPT_RETURNTRANSFER) )
{
$this->option(CURLOPT_RETURNTRANSFER, true);
}
if ( !$this->option(CURLOPT_SSL_VERIFYPEER) )
{
$this->option(CURLOPT_SSL_VERIFYPEER, false);
}
if ( !$this->option(CURLOPT_SSL_VERIFYHOST) )
{
$this->option(CURLOPT_SSL_VERIFYHOST, false);
}
if ( !$this->option(CURLOPT_CUSTOMREQUEST) )
{
$this->option(CURLOPT_CUSTOMREQUEST, 'POST');
}
if ( $this->headers )
{
$this->option(CURLOPT_HTTPHEADER, $this->headers);
}
$this->setupCurlOptions();
$this->response = curl_exec($this->ch);
$this->info = curl_getinfo($this->ch);
if ( $this->response === false )
{
$this->error = curl_error($this->ch);
$this->errno = curl_errno($this->ch);
curl_close($this->ch);
return false;
}
else
{
curl_close($this->ch);
//未处理http_code。
if ( $this->info['content_type'] == 'application/json' )
{
$this->response = json_decode($this->response, true);
}
return $this->response;
}
}
public function setupCurlOptions()
{
curl_setopt_array($this->ch, $this->options);
}
public function option($key, $value = NULL)
{
if ( is_null($value) )
{
return !isset($this->options[$key]) ? null: $this->options[$key];
}
else
{
$this->options[$key] = $value;
return $this;
}
}
public function alias($key, $value)
{
$this->alias[$key] = $value;
}
protected function encode($str)
{
$trans = array("+" => "-", "/" => "_");
return strtr(base64_encode($str), $trans);
}
public function __get($key)
{
return $this->$key;
}
public function offsetExists($key)
{
//check response;
}
public function offsetGet($key)
{
return $this->stat($key);
}
public function offsetSet($key, $value)
{
//move or copy
}
public function offsetUnset($key)
{
return $this->delete();
}
}
//demo
$accessKey = '';
$secretKey = '';
$bucket = "phpsdk";
$sdk = new SDK($accessKey, $secretKey, $bucket);
$test = $sdk['test.jpg']; // $sdk->stat('test.jpg'); or $sdk->stat('your bucket|your file name');