1 | <?php |
||
24 | class HttpClient |
||
25 | { |
||
26 | |||
27 | /** |
||
28 | * cURL handle. |
||
29 | * |
||
30 | * @var resource |
||
31 | */ |
||
32 | protected $ch; |
||
33 | |||
34 | /** |
||
35 | * Store API URL. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $url; |
||
40 | |||
41 | /** |
||
42 | * Consumer key. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $consumerKey; |
||
47 | |||
48 | /** |
||
49 | * Consumer secret. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $consumerSecret; |
||
54 | |||
55 | /** |
||
56 | * Client options. |
||
57 | * |
||
58 | * @var Options |
||
59 | */ |
||
60 | protected $options; |
||
61 | |||
62 | /** |
||
63 | * Request. |
||
64 | * |
||
65 | * @var Request |
||
66 | */ |
||
67 | private $request; |
||
68 | |||
69 | /** |
||
70 | * Response. |
||
71 | * |
||
72 | * @var Response |
||
73 | */ |
||
74 | private $response; |
||
75 | |||
76 | /** |
||
77 | * Response headers. |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | private $responseHeaders; |
||
82 | |||
83 | /** |
||
84 | * Initialize HTTP client. |
||
85 | * |
||
86 | * @param string $url Store URL. |
||
87 | * @param string $consumerKey Consumer key. |
||
88 | * @param string $consumerSecret Consumer Secret. |
||
89 | * @param array $options Client options. |
||
90 | */ |
||
91 | public function __construct($url, $consumerKey, $consumerSecret, $options) |
||
92 | { |
||
93 | if (!\function_exists('curl_version')) { |
||
94 | throw new HttpClientException('cURL is NOT installed on this server', -1, new Request(), new Response()); |
||
95 | } |
||
96 | |||
97 | $this->options = new Options($options); |
||
98 | $this->url = $this->buildApiUrl($url); |
||
99 | $this->consumerKey = $consumerKey; |
||
100 | $this->consumerSecret = $consumerSecret; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Check if is under SSL. |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | protected function isSsl() |
||
109 | { |
||
110 | return 'https://' === \substr($this->url, 0, 8); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Build API URL. |
||
115 | * |
||
116 | * @param string $url Store URL. |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | protected function buildApiUrl($url) |
||
121 | { |
||
122 | $api = $this->options->isWPAPI() ? $this->options->apiPrefix() : '/wc-api/'; |
||
123 | |||
124 | return \rtrim($url, '/') . $api . $this->options->getVersion() . '/'; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Build URL. |
||
129 | * |
||
130 | * @param string $url URL. |
||
131 | * @param array $parameters Query string parameters. |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | protected function buildUrlQuery($url, $parameters = []) |
||
143 | |||
144 | /** |
||
145 | * Authenticate. |
||
146 | * |
||
147 | * @param string $url Request URL. |
||
148 | * @param string $method Request method. |
||
149 | * @param array $parameters Request parameters. |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | protected function authenticate($url, $method, $parameters = []) |
||
166 | |||
167 | /** |
||
168 | * Setup method. |
||
169 | * |
||
170 | * @param string $method Request method. |
||
171 | */ |
||
172 | protected function setupMethod($method) |
||
180 | |||
181 | /** |
||
182 | * Get request headers. |
||
183 | * |
||
184 | * @param bool $sendData If request send data or not. |
||
185 | * |
||
186 | * @return array |
||
187 | */ |
||
188 | protected function getRequestHeaders($sendData = false) |
||
189 | { |
||
190 | $headers = [ |
||
191 | 'Accept' => 'application/json', |
||
192 | 'User-Agent' => $this->options->userAgent() . '/' . Client::VERSION, |
||
193 | ]; |
||
194 | |||
195 | if ($sendData) { |
||
196 | $headers['Content-Type'] = 'application/json;charset=utf-8'; |
||
197 | } |
||
198 | |||
199 | return $headers; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Create request. |
||
204 | * |
||
205 | * @param string $endpoint Request endpoint. |
||
206 | * @param string $method Request method. |
||
207 | * @param array $data Request data. |
||
208 | * @param array $parameters Request parameters. |
||
209 | * |
||
210 | * @return Request |
||
211 | */ |
||
212 | protected function createRequest($endpoint, $method, $data = [], $parameters = []) |
||
213 | { |
||
214 | $body = ''; |
||
215 | $url = $this->url . $endpoint; |
||
216 | $hasData = !empty($data); |
||
217 | |||
218 | // Setup authentication. |
||
219 | $parameters = $this->authenticate($url, $method, $parameters); |
||
220 | |||
221 | // Setup method. |
||
222 | $this->setupMethod($method); |
||
223 | |||
224 | // Include post fields. |
||
225 | if ($hasData) { |
||
226 | $body = \json_encode($data); |
||
227 | \curl_setopt($this->ch, CURLOPT_POSTFIELDS, $body); |
||
228 | } |
||
229 | |||
230 | $this->request = new Request($this->buildUrlQuery($url, $parameters), $method, $parameters, $this->getRequestHeaders($hasData), $body); |
||
231 | |||
232 | return $this->getRequest(); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Get response headers. |
||
237 | * |
||
238 | * @return array |
||
239 | */ |
||
240 | protected function getResponseHeaders() |
||
259 | |||
260 | /** |
||
261 | * Create response. |
||
262 | * |
||
263 | * @return Response |
||
264 | */ |
||
265 | protected function createResponse() |
||
285 | |||
286 | /** |
||
287 | * Set default cURL settings. |
||
288 | */ |
||
289 | protected function setDefaultCurlSettings() |
||
308 | |||
309 | /** |
||
310 | * Look for errors in the request. |
||
311 | * |
||
312 | * @param array $parsedResponse Parsed body response. |
||
313 | */ |
||
314 | protected function lookForErrors($parsedResponse) |
||
331 | |||
332 | /** |
||
333 | * Process response. |
||
334 | * |
||
335 | * @return array |
||
336 | */ |
||
337 | protected function processResponse() |
||
357 | |||
358 | /** |
||
359 | * Make requests. |
||
360 | * |
||
361 | * @param string $endpoint Request endpoint. |
||
362 | * @param string $method Request method. |
||
363 | * @param array $data Request data. |
||
364 | * @param array $parameters Request parameters. |
||
365 | * |
||
366 | * @return array |
||
367 | */ |
||
368 | public function request($endpoint, $method, $data = [], $parameters = []) |
||
391 | |||
392 | /** |
||
393 | * Get request data. |
||
394 | * |
||
395 | * @return Request |
||
396 | */ |
||
397 | public function getRequest() |
||
401 | |||
402 | /** |
||
403 | * Get response data. |
||
404 | * |
||
405 | * @return Response |
||
406 | */ |
||
407 | public function getResponse() |
||
411 | } |
||
412 |