|
1 | 1 | <?php |
2 | 2 | namespace PHPCurl\CurlHttp; |
3 | 3 |
|
4 | | -use \PHPCurl\CurlWrapper\Curl; |
5 | | - |
6 | 4 | /** |
7 | 5 | * Minimalistic HTTP client |
8 | 6 | */ |
9 | | -class HttpClient |
| 7 | +class HttpClient implements HttpClientInterface |
10 | 8 | { |
11 | | - const USE_EXCEPTIONS = true; |
12 | | - |
13 | 9 | /** |
14 | | - * Numer of attempts to make per each call |
15 | | - * @var int |
| 10 | + * @var ExecutorInterface |
16 | 11 | */ |
17 | | - protected $attempts = 1; |
| 12 | + private $executor; |
18 | 13 |
|
19 | 14 | /** |
20 | | - * @var array |
| 15 | + * HttpClient constructor. |
| 16 | + * @param ExecutorInterface $executor |
21 | 17 | */ |
22 | | - private $userOptions = array(); |
| 18 | + public function __construct(ExecutorInterface $executor = null) |
| 19 | + { |
| 20 | + $this->executor = $executor ?: new Executor(); |
| 21 | + } |
23 | 22 |
|
24 | 23 | /** |
25 | 24 | * HTTP GET |
26 | | - * @param string $url Goes to curl_init() |
27 | | - * @param array $headers Same as CURLOPT_HEADER |
| 25 | + * @param string $url Goes to curl_init() |
| 26 | + * @param array $headers Same as CURLOPT_HEADER |
28 | 27 | * @return HttpResponse |
29 | 28 | */ |
30 | | - public function get($url, array $headers = null) |
| 29 | + public function get($url, array $headers = []) |
31 | 30 | { |
32 | | - $opt = array(); |
33 | | - if ($headers) { |
34 | | - $opt[CURLOPT_HTTPHEADER] = $headers; |
35 | | - } |
36 | | - return $this->exec($url, $opt); |
| 31 | + return $this->executor->execute( |
| 32 | + $url, |
| 33 | + [ |
| 34 | + CURLOPT_HTTPHEADER => $headers, |
| 35 | + ] |
| 36 | + ); |
37 | 37 | } |
38 | 38 |
|
39 | 39 | /** |
40 | 40 | * HTTP HEAD (implemented using CURLOPT_NOBODY) |
41 | | - * @param string $url Goes to curl_init() |
42 | | - * @param array $headers Same as CURLOPT_HEADER |
| 41 | + * @param string $url Goes to curl_init() |
| 42 | + * @param array $headers Same as CURLOPT_HEADER |
43 | 43 | * @return HttpResponse |
44 | 44 | */ |
45 | | - public function head($url, array $headers = null) |
| 45 | + public function head($url, array $headers = []) |
46 | 46 | { |
47 | | - $opt[CURLOPT_NOBODY] = true; |
48 | | - if ($headers !== null) { |
49 | | - $opt[CURLOPT_HTTPHEADER] = $headers; |
50 | | - } |
51 | | - return $this->exec($url, $opt); |
| 47 | + return $this->executor->execute( |
| 48 | + $url, |
| 49 | + [ |
| 50 | + CURLOPT_NOBODY => true, |
| 51 | + CURLOPT_HTTPHEADER => $headers, |
| 52 | + ] |
| 53 | + ); |
52 | 54 | } |
53 | 55 |
|
54 | 56 | /** |
55 | 57 | * HTTP POST |
56 | | - * @param string $url Goes to curl_init() |
57 | | - * @param string|array $data Same as CURLOPT_POSTFIELDS |
58 | | - * @param array $headers Same as CURLOPT_HEADER |
| 58 | + * @param string $url Goes to curl_init() |
| 59 | + * @param string|array $data Same as CURLOPT_POSTFIELDS |
| 60 | + * @param array $headers Same as CURLOPT_HEADER |
59 | 61 | * @return HttpResponse |
60 | 62 | */ |
61 | | - public function post($url, $data = null, array $headers = null) |
| 63 | + public function post($url, $data = '', array $headers = []) |
62 | 64 | { |
63 | | - $opt[CURLOPT_POST] = true; |
64 | | - if ($data !== null) { |
65 | | - $opt[CURLOPT_POSTFIELDS] = $data; |
66 | | - } |
67 | | - if ($headers !== null) { |
68 | | - $opt[CURLOPT_HTTPHEADER] = $headers; |
69 | | - } |
70 | | - return $this->exec($url, $opt); |
| 65 | + return $this->executor->execute( |
| 66 | + $url, |
| 67 | + [ |
| 68 | + CURLOPT_POST => true, |
| 69 | + CURLOPT_HTTPHEADER => $headers, |
| 70 | + CURLOPT_POSTFIELDS => $data, |
| 71 | + ] |
| 72 | + ); |
71 | 73 | } |
72 | 74 |
|
73 | 75 | /** |
74 | 76 | * HTTP PUT |
75 | | - * @param string $url Goes to curl_init() |
76 | | - * @param string|array $data Same as CURLOPT_POSTFIELDS |
77 | | - * @param array $headers Same as CURLOPT_HEADER |
| 77 | + * @param string $url Goes to curl_init() |
| 78 | + * @param string|array $data Same as CURLOPT_POSTFIELDS |
| 79 | + * @param array $headers Same as CURLOPT_HEADER |
78 | 80 | * @return HttpResponse |
79 | 81 | */ |
80 | | - public function put($url, $data = null, array $headers = null) |
| 82 | + public function put($url, $data = '', array $headers = []) |
81 | 83 | { |
82 | | - return $this->request('PUT', $url, $data, $headers); |
| 84 | + return $this->executor->execute( |
| 85 | + $url, |
| 86 | + [ |
| 87 | + CURLOPT_CUSTOMREQUEST => 'PUT', |
| 88 | + CURLOPT_HTTPHEADER => $headers, |
| 89 | + CURLOPT_POSTFIELDS => $data, |
| 90 | + ] |
| 91 | + ); |
83 | 92 | } |
84 | 93 |
|
85 | 94 | /** |
86 | 95 | * HTTP DELETE |
87 | | - * @param string $url Goes to curl_init() |
88 | | - * @param array $headers Same as CURLOPT_HEADER |
89 | | - * @return HttpResponse |
90 | | - */ |
91 | | - public function delete($url, array $headers = null) |
92 | | - { |
93 | | - return $this->request('DELETE', $url, null, $headers); |
94 | | - } |
95 | | - |
96 | | - /** |
97 | | - * Custom HTTP method |
98 | | - * @param string $method Goes to CURLOPT_CUSTOMREQUEST |
99 | | - * @param string $url Goes to curl_init() |
100 | | - * @param string|array $data Goes to CURLOPT_POSTFIELDS |
101 | | - * @param array $headers Goes to CURLOPT_HEADER |
102 | | - * @return HttpResponse |
103 | | - */ |
104 | | - public function request($method, $url, $data = null, array $headers = null) |
105 | | - { |
106 | | - $opt[CURLOPT_CUSTOMREQUEST] = $method; |
107 | | - if ($headers !== null) { |
108 | | - $opt[CURLOPT_HTTPHEADER] = $headers; |
109 | | - } |
110 | | - if ($data !== null) { |
111 | | - $opt[CURLOPT_POSTFIELDS] = $data; |
112 | | - } |
113 | | - return $this->exec($url, $opt); |
114 | | - } |
115 | | - |
116 | | - /** |
117 | | - * Set additional CURL options to pass with each request |
118 | | - * @param array $userOptions Format is the same as curl_setopt_array(). |
119 | | - * Pass an empty array to clear. |
120 | | - */ |
121 | | - public function setOptions(array $userOptions) |
122 | | - { |
123 | | - $this->userOptions = $userOptions; |
124 | | - } |
125 | | - |
126 | | - /** |
127 | | - * Init curl with $url, set $options, execute, return response |
128 | | - * @param string $url Goes to curl_init() |
129 | | - * @param array $options Goes to curl_setopt() |
130 | | - * @param Curl $curl |
| 96 | + * @param string $url Goes to curl_init() |
| 97 | + * @param array $headers Same as CURLOPT_HEADER |
131 | 98 | * @return HttpResponse |
132 | 99 | */ |
133 | | - public function exec($url, array $options, Curl $curl = null) |
| 100 | + public function delete($url, array $headers = []) |
134 | 101 | { |
135 | | - $options[CURLOPT_RETURNTRANSFER] = true; |
136 | | - $options[CURLOPT_HEADER] = true; |
137 | | - |
138 | | - $curl = $curl ?: new Curl(); |
139 | | - $curl->init($url); |
140 | | - $curl->setOptArray(array_replace_recursive( |
141 | | - $this->userOptions, |
142 | | - $options |
143 | | - )); |
144 | | - |
145 | | - $response = $curl->exec($this->attempts, self::USE_EXCEPTIONS); |
146 | | - |
147 | | - $info = $curl->getInfo(); |
148 | | - $code = $info['http_code']; |
149 | | - $headerSize = $info['header_size']; |
150 | | - $headers = substr($response, 0, $headerSize); |
151 | | - $headersArray = preg_split("/\r\n/", $headers, -1, PREG_SPLIT_NO_EMPTY); |
152 | | - $body = substr($response, $headerSize); |
153 | | - return new HttpResponse($code, $headersArray, $body); |
| 102 | + return $this->executor->execute( |
| 103 | + $url, |
| 104 | + [ |
| 105 | + CURLOPT_CUSTOMREQUEST => 'DELETE', |
| 106 | + CURLOPT_HTTPHEADER => $headers, |
| 107 | + ] |
| 108 | + ); |
154 | 109 | } |
155 | 110 | } |
0 commit comments