1 | <?php |
||
8 | final class Client |
||
9 | { |
||
10 | 66 | public static function get($url, array $headers = array()) |
|
15 | |||
16 | 48 | public static function post($url, $body, array $headers = array()) |
|
21 | |||
22 | public static function multipartPost( |
||
23 | $url, |
||
24 | $fields, |
||
25 | $name, |
||
26 | $fileName, |
||
27 | $fileBody, |
||
28 | $mimeType = null, |
||
29 | array $headers = array() |
||
30 | ) { |
||
31 | $data = array(); |
||
32 | $mimeBoundary = md5(microtime()); |
||
33 | |||
34 | foreach ($fields as $key => $val) { |
||
35 | array_push($data, '--' . $mimeBoundary); |
||
36 | array_push($data, "Content-Disposition: form-data; name=\"$key\""); |
||
37 | array_push($data, ''); |
||
38 | array_push($data, $val); |
||
39 | } |
||
40 | |||
41 | array_push($data, '--' . $mimeBoundary); |
||
42 | $finalMimeType = empty($mimeType) ? 'application/octet-stream' : $mimeType; |
||
43 | $finalFileName = self::escapeQuotes($fileName); |
||
44 | array_push($data, "Content-Disposition: form-data; name=\"$name\"; filename=\"$finalFileName\""); |
||
45 | array_push($data, "Content-Type: $finalMimeType"); |
||
46 | array_push($data, ''); |
||
47 | array_push($data, $fileBody); |
||
48 | |||
49 | array_push($data, '--' . $mimeBoundary . '--'); |
||
50 | array_push($data, ''); |
||
51 | |||
52 | $body = implode("\r\n", $data); |
||
53 | $contentType = 'multipart/form-data; boundary=' . $mimeBoundary; |
||
54 | $headers['Content-Type'] = $contentType; |
||
55 | $request = new Request('POST', $url, $headers, $body); |
||
56 | return self::sendRequest($request); |
||
57 | } |
||
58 | |||
59 | 108 | private static function userAgent() |
|
73 | |||
74 | 108 | public static function sendRequest($request) |
|
75 | { |
||
76 | 108 | $t1 = microtime(true); |
|
77 | 108 | $ch = curl_init(); |
|
78 | $options = array( |
||
79 | 108 | CURLOPT_USERAGENT => self::userAgent(), |
|
80 | 108 | CURLOPT_RETURNTRANSFER => true, |
|
81 | 108 | CURLOPT_SSL_VERIFYPEER => false, |
|
82 | 108 | CURLOPT_SSL_VERIFYHOST => false, |
|
83 | 108 | CURLOPT_HEADER => true, |
|
84 | 108 | CURLOPT_NOBODY => false, |
|
85 | 108 | CURLOPT_CUSTOMREQUEST => $request->method, |
|
86 | 108 | CURLOPT_URL => $request->url |
|
87 | 108 | ); |
|
88 | |||
89 | // Handle open_basedir & safe mode |
||
90 | 108 | if (!ini_get('safe_mode') && !ini_get('open_basedir')) { |
|
91 | 108 | $options[CURLOPT_FOLLOWLOCATION] = true; |
|
92 | 108 | } |
|
93 | |||
94 | 108 | if (!empty($request->headers)) { |
|
95 | 51 | $headers = array(); |
|
96 | 51 | foreach ($request->headers as $key => $val) { |
|
97 | 51 | array_push($headers, "$key: $val"); |
|
98 | 51 | } |
|
99 | 51 | $options[CURLOPT_HTTPHEADER] = $headers; |
|
100 | 51 | } |
|
101 | 108 | curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); |
|
102 | |||
103 | 108 | if (!empty($request->body)) { |
|
104 | 21 | $options[CURLOPT_POSTFIELDS] = $request->body; |
|
105 | 21 | } |
|
106 | 108 | curl_setopt_array($ch, $options); |
|
107 | 108 | $result = curl_exec($ch); |
|
108 | 108 | $t2 = microtime(true); |
|
109 | 108 | $duration = round($t2-$t1, 3); |
|
110 | 108 | $ret = curl_errno($ch); |
|
111 | 108 | if ($ret !== 0) { |
|
112 | 6 | $r = new Response(-1, $duration, array(), null, curl_error($ch)); |
|
113 | 6 | curl_close($ch); |
|
114 | 6 | return $r; |
|
115 | } |
||
116 | 108 | $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
117 | 108 | $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
|
118 | 108 | $headers = self::parseHeaders(substr($result, 0, $header_size)); |
|
119 | 108 | $body = substr($result, $header_size); |
|
120 | 108 | curl_close($ch); |
|
121 | 108 | return new Response($code, $duration, $headers, $body, null); |
|
122 | } |
||
123 | |||
124 | 108 | private static function parseHeaders($raw) |
|
137 | |||
138 | private static function escapeQuotes($str) |
||
139 | { |
||
144 | } |
||
145 |