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