1 | <?php |
||
33 | class Http |
||
34 | { |
||
35 | /** |
||
36 | * Http client. |
||
37 | * |
||
38 | * @var HttpClient |
||
39 | */ |
||
40 | protected $client; |
||
41 | |||
42 | /** |
||
43 | * The middlewares. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $middlewares = []; |
||
48 | |||
49 | /** |
||
50 | * Guzzle client default settings. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected static $defaults = []; |
||
55 | |||
56 | /** |
||
57 | * Set guzzle default settings. |
||
58 | * |
||
59 | * @param array $defaults |
||
60 | */ |
||
61 | 4 | public static function setDefaultOptions($defaults = []) |
|
65 | |||
66 | /** |
||
67 | * Return current guzzle default settings. |
||
68 | * |
||
69 | * @return array |
||
70 | */ |
||
71 | 1 | public static function getDefaultOptions() |
|
75 | |||
76 | /** |
||
77 | * GET request. |
||
78 | * |
||
79 | * @param string $url |
||
80 | * @param array $options |
||
81 | * |
||
82 | * @return ResponseInterface |
||
83 | * |
||
84 | * @throws HttpException |
||
85 | */ |
||
86 | 2 | public function get($url, array $options = []) |
|
87 | { |
||
88 | 2 | return $this->request($url, 'GET', ['query' => $options]); |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * POST request. |
||
93 | * |
||
94 | * @param string $url |
||
95 | * @param array|string $options |
||
96 | * |
||
97 | * @return ResponseInterface |
||
98 | * |
||
99 | * @throws HttpException |
||
100 | */ |
||
101 | 1 | public function post($url, $options = []) |
|
102 | { |
||
103 | 1 | $key = is_array($options) ? 'form_params' : 'body'; |
|
104 | |||
105 | 1 | return $this->request($url, 'POST', [$key => $options]); |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * JSON request. |
||
110 | * |
||
111 | * @param string $url |
||
112 | * @param string|array $options |
||
113 | * @param int $encodeOption |
||
114 | * |
||
115 | * @return ResponseInterface |
||
116 | * |
||
117 | * @throws HttpException |
||
118 | */ |
||
119 | 1 | public function json($url, $options = [], $encodeOption = JSON_UNESCAPED_UNICODE) |
|
120 | { |
||
121 | 1 | is_array($options) && $options = json_encode($options, $encodeOption); |
|
122 | |||
123 | 1 | return $this->request($url, 'POST', ['body' => $options, 'headers' => ['content-type' => 'application/json']]); |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * Upload file. |
||
128 | * |
||
129 | * @param string $url |
||
130 | * @param array $files |
||
131 | * @param array $form |
||
132 | * |
||
133 | * @return ResponseInterface |
||
134 | * |
||
135 | * @throws HttpException |
||
136 | */ |
||
137 | 1 | public function upload($url, array $files = [], array $form = [], array $queries = []) |
|
138 | { |
||
139 | 1 | $multipart = []; |
|
140 | |||
141 | 1 | foreach ($files as $name => $path) { |
|
142 | 1 | $multipart[] = [ |
|
143 | 1 | 'name' => $name, |
|
144 | 1 | 'contents' => fopen($path, 'r'), |
|
145 | ]; |
||
146 | 1 | } |
|
147 | |||
148 | 1 | foreach ($form as $name => $contents) { |
|
149 | 1 | $multipart[] = compact('name', 'contents'); |
|
150 | 1 | } |
|
151 | |||
152 | 1 | return $this->request($url, 'POST', ['query' => $queries, 'multipart' => $multipart]); |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Set GuzzleHttp\Client. |
||
157 | * |
||
158 | * @param \GuzzleHttp\Client $client |
||
159 | * |
||
160 | * @return Http |
||
161 | */ |
||
162 | 6 | public function setClient(HttpClient $client) |
|
163 | { |
||
164 | 6 | $this->client = $client; |
|
165 | |||
166 | 6 | return $this; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * Return GuzzleHttp\Client instance. |
||
171 | * |
||
172 | * @return \GuzzleHttp\Client |
||
173 | */ |
||
174 | 4 | public function getClient() |
|
175 | { |
||
176 | 4 | if (!($this->client instanceof HttpClient)) { |
|
177 | 2 | $this->client = new HttpClient(); |
|
178 | 2 | } |
|
179 | |||
180 | 4 | return $this->client; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Add a middleware. |
||
185 | * |
||
186 | * @param callable $middleware |
||
187 | * |
||
188 | * @return $this |
||
189 | */ |
||
190 | 9 | public function addMiddleware(callable $middleware) |
|
191 | { |
||
192 | 9 | array_push($this->middlewares, $middleware); |
|
193 | |||
194 | 9 | return $this; |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * Return all middlewares. |
||
199 | * |
||
200 | * @return array |
||
201 | */ |
||
202 | 9 | public function getMiddlewares() |
|
203 | { |
||
204 | 9 | return $this->middlewares; |
|
205 | } |
||
206 | |||
207 | /** |
||
208 | * Make a request. |
||
209 | * |
||
210 | * @param string $url |
||
211 | * @param string $method |
||
212 | * @param array $options |
||
213 | * |
||
214 | * @return ResponseInterface |
||
215 | * |
||
216 | * @throws HttpException |
||
217 | */ |
||
218 | 3 | public function request($url, $method = 'GET', $options = []) |
|
239 | |||
240 | /** |
||
241 | * @param \Psr\Http\Message\ResponseInterface|string $body |
||
242 | * |
||
243 | * @return mixed |
||
244 | * |
||
245 | * @throws \EasyWeChat\Core\Exceptions\HttpException |
||
246 | */ |
||
247 | 10 | public function parseJSON($body) |
|
270 | |||
271 | /** |
||
272 | * Filter the invalid JSON string. |
||
273 | * |
||
274 | * @param \Psr\Http\Message\StreamInterface|string $invalidJSON |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | 10 | protected function fuckTheWeChatInvalidJSON($invalidJSON) |
|
282 | |||
283 | /** |
||
284 | * Build a handler. |
||
285 | * |
||
286 | * @return HandlerStack |
||
287 | */ |
||
288 | 3 | protected function getHandler() |
|
298 | } |
||
299 |