1
|
|
|
<?php |
2
|
|
|
namespace Qiniu\Http; |
3
|
|
|
|
4
|
|
|
use Qiniu\Config; |
5
|
|
|
use Qiniu\Http\Middleware; |
6
|
|
|
|
7
|
|
|
final class Client |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
69 |
|
* @param $url |
11
|
|
|
* @param array $headers |
12
|
69 |
|
* @param RequestOptions $opt |
13
|
69 |
|
* @return Response |
14
|
|
|
*/ |
15
|
|
|
public static function get($url, array $headers = array(), $opt = null) |
16
|
|
|
{ |
17
|
|
|
$request = new Request('GET', $url, $headers, null, $opt); |
18
|
|
|
return self::sendRequestWithMiddleware($request); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
57 |
|
* @param $url |
23
|
|
|
* @param array $headers |
24
|
57 |
|
* @param array $opt detail see {@see Request::$opt} |
25
|
57 |
|
* @return Response |
26
|
|
|
*/ |
27
|
|
|
public static function delete($url, array $headers = array(), $opt = null) |
28
|
|
|
{ |
29
|
|
|
$request = new Request('DELETE', $url, $headers, null, $opt); |
30
|
|
|
return self::sendRequest($request); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
9 |
|
* @param $url |
35
|
|
|
* @param $body |
36
|
|
|
* @param array $headers |
37
|
|
|
* @param RequestOptions $opt |
38
|
|
|
* @return Response |
39
|
|
|
*/ |
40
|
|
|
public static function post($url, $body, array $headers = array(), $opt = null) |
41
|
|
|
{ |
42
|
|
|
$request = new Request('POST', $url, $headers, $body, $opt); |
43
|
9 |
|
return self::sendRequest($request); |
44
|
9 |
|
} |
45
|
|
|
|
46
|
9 |
|
/** |
47
|
9 |
|
* @param $url |
48
|
9 |
|
* @param $body |
49
|
9 |
|
* @param array $headers |
50
|
9 |
|
* @param RequestOptions $opt |
51
|
9 |
|
* @return Response |
52
|
|
|
*/ |
53
|
9 |
|
public static function PUT($url, $body, array $headers = array(), $opt = null) |
54
|
9 |
|
{ |
55
|
9 |
|
$request = new Request('PUT', $url, $headers, $body, $opt); |
56
|
9 |
|
return self::sendRequest($request); |
57
|
9 |
|
} |
58
|
9 |
|
|
59
|
9 |
|
/** |
60
|
|
|
* @param $url |
61
|
9 |
|
* @param array $fields |
62
|
9 |
|
* @param string $name |
63
|
|
|
* @param string $fileName |
64
|
9 |
|
* @param $fileBody |
65
|
|
|
* @param null $mimeType |
|
|
|
|
66
|
9 |
|
* @param array $headers |
67
|
9 |
|
* @param RequestOptions $opt |
68
|
9 |
|
* @return Response |
69
|
9 |
|
*/ |
70
|
|
|
public static function multipartPost( |
71
|
|
|
$url, |
72
|
102 |
|
$fields, |
73
|
|
|
$name, |
74
|
102 |
|
$fileName, |
75
|
|
|
$fileBody, |
76
|
102 |
|
$mimeType = null, |
77
|
102 |
|
$headers = array(), |
78
|
|
|
$opt = null |
79
|
102 |
|
) { |
80
|
|
|
$data = array(); |
81
|
102 |
|
$mimeBoundary = md5(microtime()); |
82
|
|
|
|
83
|
102 |
|
foreach ($fields as $key => $val) { |
84
|
102 |
|
array_push($data, '--' . $mimeBoundary); |
85
|
|
|
array_push($data, "Content-Disposition: form-data; name=\"$key\""); |
86
|
|
|
array_push($data, ''); |
87
|
102 |
|
array_push($data, $val); |
88
|
|
|
} |
89
|
102 |
|
|
90
|
102 |
|
array_push($data, '--' . $mimeBoundary); |
91
|
|
|
$finalMimeType = empty($mimeType) ? 'application/octet-stream' : $mimeType; |
92
|
102 |
|
$finalFileName = self::escapeQuotes($fileName); |
93
|
102 |
|
array_push($data, "Content-Disposition: form-data; name=\"$name\"; filename=\"$finalFileName\""); |
94
|
102 |
|
array_push($data, "Content-Type: $finalMimeType"); |
95
|
102 |
|
array_push($data, ''); |
96
|
102 |
|
array_push($data, $fileBody); |
97
|
102 |
|
|
98
|
102 |
|
array_push($data, '--' . $mimeBoundary . '--'); |
99
|
102 |
|
array_push($data, ''); |
100
|
102 |
|
|
101
|
|
|
$body = implode("\r\n", $data); |
102
|
102 |
|
$contentType = 'multipart/form-data; boundary=' . $mimeBoundary; |
103
|
102 |
|
$headers['Content-Type'] = $contentType; |
104
|
102 |
|
$request = new Request('POST', $url, $headers, $body, $opt); |
105
|
102 |
|
return self::sendRequest($request); |
106
|
69 |
|
} |
107
|
69 |
|
|
108
|
69 |
|
private static function userAgent() |
109
|
69 |
|
{ |
110
|
69 |
|
$sdkInfo = "QiniuPHP/" . Config::SDK_VER; |
111
|
69 |
|
|
112
|
102 |
|
$systemInfo = php_uname("s"); |
113
|
102 |
|
$machineInfo = php_uname("m"); |
114
|
39 |
|
|
115
|
39 |
|
$envInfo = "($systemInfo/$machineInfo)"; |
116
|
102 |
|
|
117
|
102 |
|
$phpVer = phpversion(); |
118
|
102 |
|
|
119
|
102 |
|
$ua = "$sdkInfo $envInfo PHP/$phpVer"; |
120
|
102 |
|
return $ua; |
121
|
102 |
|
} |
122
|
3 |
|
|
123
|
3 |
|
/** |
124
|
3 |
|
* @param Request $request |
125
|
|
|
* @return Response |
126
|
102 |
|
*/ |
127
|
102 |
|
public static function sendRequestWithMiddleware($request) |
128
|
102 |
|
{ |
129
|
102 |
|
$middlewares = $request->opt->middlewares; |
130
|
102 |
|
$handle = Middleware\compose($middlewares, function ($req) { |
131
|
102 |
|
return Client::sendRequest($req); |
132
|
|
|
}); |
133
|
|
|
return $handle($request); |
134
|
102 |
|
} |
135
|
|
|
|
136
|
102 |
|
/** |
137
|
102 |
|
* @param Request $request |
138
|
102 |
|
* @return Response |
139
|
102 |
|
*/ |
140
|
102 |
|
public static function sendRequest($request) |
141
|
102 |
|
{ |
142
|
102 |
|
$t1 = microtime(true); |
143
|
102 |
|
$ch = curl_init(); |
144
|
102 |
|
$options = array( |
145
|
102 |
|
CURLOPT_USERAGENT => self::userAgent(), |
146
|
102 |
|
CURLOPT_RETURNTRANSFER => true, |
147
|
|
|
CURLOPT_SSL_VERIFYPEER => false, |
148
|
|
|
CURLOPT_SSL_VERIFYHOST => false, |
149
|
9 |
|
CURLOPT_HEADER => true, |
150
|
|
|
CURLOPT_NOBODY => false, |
151
|
9 |
|
CURLOPT_CUSTOMREQUEST => $request->method, |
152
|
9 |
|
CURLOPT_URL => $request->url, |
153
|
9 |
|
); |
154
|
|
|
foreach ($request->opt->getCurlOpt() as $k => $v) { |
155
|
|
|
$options[$k] = $v; |
156
|
102 |
|
} |
157
|
|
|
// Handle open_basedir & safe mode |
158
|
102 |
|
if (!ini_get('safe_mode') && !ini_get('open_basedir')) { |
159
|
|
|
$options[CURLOPT_FOLLOWLOCATION] = true; |
160
|
|
|
} |
161
|
|
|
if (!empty($request->headers)) { |
162
|
|
|
$headers = array(); |
163
|
|
|
foreach ($request->headers as $key => $val) { |
164
|
|
|
array_push($headers, "$key: $val"); |
165
|
|
|
} |
166
|
|
|
$options[CURLOPT_HTTPHEADER] = $headers; |
167
|
|
|
} |
168
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); |
169
|
|
|
if (!empty($request->body)) { |
170
|
|
|
$options[CURLOPT_POSTFIELDS] = $request->body; |
171
|
|
|
} |
172
|
|
|
curl_setopt_array($ch, $options); |
173
|
|
|
$result = curl_exec($ch); |
174
|
|
|
$t2 = microtime(true); |
175
|
|
|
$duration = round($t2 - $t1, 3); |
176
|
|
|
$ret = curl_errno($ch); |
177
|
|
|
if ($ret !== 0) { |
178
|
|
|
$r = new Response(-1, $duration, array(), null, curl_error($ch)); |
179
|
|
|
curl_close($ch); |
180
|
|
|
return $r; |
181
|
|
|
} |
182
|
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
183
|
|
|
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
184
|
|
|
$headers = Header::parseRawText(substr($result, 0, $header_size)); |
|
|
|
|
185
|
|
|
$body = substr($result, $header_size); |
186
|
|
|
curl_close($ch); |
187
|
|
|
return new Response($code, $duration, $headers, $body, null); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
private static function escapeQuotes($str) |
191
|
|
|
{ |
192
|
|
|
if (is_null($str)) { |
193
|
|
|
return null; |
194
|
|
|
} |
195
|
|
|
$find = array("\\", "\""); |
196
|
|
|
$replace = array("\\\\", "\\\""); |
197
|
|
|
return str_replace($find, $replace, $str); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|