1 | <?php |
||
8 | final class Client |
||
9 | { |
||
10 | 68 | public static function get($url, array $headers = array()) |
|
15 | |||
16 | public static function delete($url, array $headers = array()) |
||
21 | |||
22 | 56 | public static function post($url, $body, array $headers = array()) |
|
27 | |||
28 | 9 | public static function multipartPost( |
|
29 | $url, |
||
30 | $fields, |
||
31 | $name, |
||
32 | $fileName, |
||
33 | $fileBody, |
||
34 | $mimeType = null, |
||
35 | array $headers = array() |
||
36 | ) { |
||
37 | 9 | $data = array(); |
|
38 | 9 | $mimeBoundary = md5(microtime()); |
|
39 | |||
40 | 9 | foreach ($fields as $key => $val) { |
|
41 | 9 | array_push($data, '--' . $mimeBoundary); |
|
42 | 9 | array_push($data, "Content-Disposition: form-data; name=\"$key\""); |
|
43 | 9 | array_push($data, ''); |
|
44 | 9 | array_push($data, $val); |
|
45 | 9 | } |
|
46 | |||
47 | 9 | array_push($data, '--' . $mimeBoundary); |
|
48 | 9 | $finalMimeType = empty($mimeType) ? 'application/octet-stream' : $mimeType; |
|
49 | 9 | $finalFileName = self::escapeQuotes($fileName); |
|
50 | 9 | array_push($data, "Content-Disposition: form-data; name=\"$name\"; filename=\"$finalFileName\""); |
|
51 | 9 | array_push($data, "Content-Type: $finalMimeType"); |
|
52 | 9 | array_push($data, ''); |
|
53 | 9 | array_push($data, $fileBody); |
|
54 | |||
55 | 9 | array_push($data, '--' . $mimeBoundary . '--'); |
|
56 | 9 | array_push($data, ''); |
|
57 | |||
58 | 9 | $body = implode("\r\n", $data); |
|
59 | 9 | $contentType = 'multipart/form-data; boundary=' . $mimeBoundary; |
|
60 | 9 | $headers['Content-Type'] = $contentType; |
|
61 | 9 | $request = new Request('POST', $url, $headers, $body); |
|
62 | 9 | return self::sendRequest($request); |
|
63 | } |
||
64 | |||
65 | 102 | private static function userAgent() |
|
79 | |||
80 | 102 | public static function sendRequest($request) |
|
129 | |||
130 | 95 | private static function parseHeaders($raw) |
|
144 | |||
145 | 9 | private static function escapeQuotes($str) |
|
146 | { |
||
147 | 9 | $find = array("\\", "\""); |
|
148 | 9 | $replace = array("\\\\", "\\\""); |
|
149 | 9 | return str_replace($find, $replace, $str); |
|
150 | } |
||
151 | |||
152 | 95 | private static function ucwordsHyphen($str) |
|
156 | } |
||
157 |