1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) Padosoft.com 2016. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Padosoft\HTTPClient; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
use Padosoft\HTTPClient\Response; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
use Psr\Log\LogLevel; |
12
|
|
|
use GuzzleHttp\Client; |
13
|
|
|
use GuzzleHttp\Exception; |
14
|
|
|
use GuzzleHttp\Exception\ClientException; |
15
|
|
|
use GuzzleHttp\Exception\RequestException; |
16
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
17
|
|
|
use GuzzleHttp\Exception\ServerException; |
18
|
|
|
use GuzzleHttp\Exception\TooManyRedirectsException; |
19
|
|
|
use GuzzleHttp\Exception\ConnectException; |
20
|
|
|
use GuzzleHttp\Exception\TransferException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class HTTPClient |
24
|
|
|
* Perform HTTP Request using Guzzle. |
25
|
|
|
* @package Padosoft\HTTPClient |
26
|
|
|
*/ |
27
|
|
|
class HTTPClient |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var \GuzzleHttp\Client |
31
|
|
|
*/ |
32
|
|
|
protected $httpclient; |
33
|
|
|
/** |
34
|
|
|
* @var \Padosoft\HTTPClient\Response |
35
|
|
|
*/ |
36
|
|
|
protected $response; |
37
|
|
|
/** |
38
|
|
|
* @var \Psr\Log\LoggerInterface |
39
|
|
|
*/ |
40
|
|
|
protected $log; |
41
|
|
|
/** |
42
|
|
|
* @var \Padosoft\HTTPClient\RequestHelper |
43
|
|
|
*/ |
44
|
|
|
protected $requestHelper; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* HTTPClient constructor. |
48
|
|
|
* @param Client $objguzzle |
49
|
|
|
* @param RequestHelper $requestHelper |
50
|
|
|
* @param LoggerInterface|null $log |
51
|
|
|
*/ |
52
|
15 |
|
public function __construct(Client $objguzzle, RequestHelper $requestHelper, LoggerInterface $log=null) |
53
|
|
|
{ |
54
|
15 |
|
$this->httpclient = $objguzzle; |
55
|
15 |
|
$this->requestHelper=$requestHelper; |
56
|
15 |
|
$this->response = new Response(); |
57
|
15 |
|
$this->log = $log; |
58
|
15 |
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Send a HTTP Request and return Response or thrown Exception. |
64
|
|
|
* |
65
|
|
|
* @param string $method HTTP verb |
66
|
|
|
* @param string null $uri |
67
|
|
|
* @return \Padosoft\HTTPClient\Response |
68
|
|
|
* @throws Exception |
69
|
15 |
|
* @throws \Exception |
70
|
|
|
*/ |
71
|
|
|
public function sendRequest($method ,$uri = null) |
72
|
15 |
|
{ |
73
|
|
|
|
74
|
|
|
$this->reset(); |
75
|
|
|
|
76
|
15 |
|
try { |
77
|
|
|
//log request |
78
|
|
|
$this->setLog(LogLevel::INFO, 'Try to send Request: Method: '.$method.' uri: '.$uri, $this->requestHelper->options); |
|
|
|
|
79
|
15 |
|
|
80
|
|
|
//send request |
81
|
|
|
$this->response->psr7response = $this->httpclient->request($method, $uri, $this->requestHelper->options ); |
|
|
|
|
82
|
9 |
|
|
83
|
|
|
//log headers |
84
|
|
|
$this->setLog(LogLevel::INFO, 'Request send with Headers: '.$this->getHeadersString($this->response->psr7response), $this->requestHelper->options); |
|
|
|
|
85
|
|
|
|
86
|
|
|
// To retrive body, do not use ->getContents() |
87
|
|
|
// it turns out that this gets the “remaining contents of the body as a string” |
88
|
|
|
// and since the middleware already accessed the body (for example in order to log it), |
89
|
|
|
// there isn’t any content remaining to retrieve. |
90
|
9 |
|
// The correct way to access the response content is to cast the body to a string. |
91
|
|
|
// See: https://michaelstivala.com/logging-guzzle-requests/ |
92
|
|
|
$this->response->body = (string) $this->response->psr7response->getBody(); |
|
|
|
|
93
|
9 |
|
|
94
|
|
|
//log status code |
95
|
|
|
$this->response->status_code = $this->response->psr7response->getStatusCode(); |
|
|
|
|
96
|
9 |
|
|
97
|
|
|
//log response |
98
|
15 |
|
$this->setLog(LogLevel::INFO,'Response ',['status code'=>$this->response->status_code,'body'=>$this->response->body]); |
|
|
|
|
99
|
|
|
|
100
|
6 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
101
|
6 |
|
|
102
|
6 |
|
$responseStatusCode = $this->getResponseStatusCodeFromException($e); |
103
|
6 |
|
$this->setLog(LogLevel::ERROR,'\GuzzleHttp\Exception\ClientException',['message'=>$e->getMessage(),'path request'=>$e->getRequest()->getUri()->getPath(),'method'=>$e->getRequest()->getMethod(),'response status code'=>$responseStatusCode]); |
104
|
|
|
$this->setLog(LogLevel::DEBUG,'\GuzzleHttp\Exception\ClientException',['trace'=>$e->getTraceAsString()]); |
105
|
3 |
|
throw new \GuzzleHttp\Exception\ClientException($e->getMessage().' '.$responseStatusCode,$e->getRequest(),$e->getResponse(),$e,$e->getHandlerContext()); |
106
|
|
|
|
107
|
3 |
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
108
|
3 |
|
|
109
|
3 |
|
$responseStatusCode = $this->getResponseStatusCodeFromException($e); |
110
|
3 |
|
$this->setLog(LogLevel::ERROR,'\GuzzleHttp\Exception\ServerException',['message'=>$e->getMessage(),'path request'=>$e->getRequest()->getUri()->getPath(),'method'=>$e->getRequest()->getMethod(),'response status code'=>$responseStatusCode]); |
111
|
|
|
$this->setLog(LogLevel::DEBUG,'\GuzzleHttp\Exception\ServerException',['trace'=>$e->getTraceAsString()]); |
112
|
|
|
throw new \GuzzleHttp\Exception\ServerException($e->getMessage().' '.$responseStatusCode,$e->getRequest(),$e->getResponse(),$e,$e->getHandlerContext()); |
113
|
3 |
|
|
114
|
|
|
|
115
|
3 |
|
} catch (\GuzzleHttp\Exception\BadResponseException $e) { |
116
|
3 |
|
|
117
|
3 |
|
$responseStatusCode = $this->getResponseStatusCodeFromException($e); |
118
|
3 |
|
$this->setLog(LogLevel::ERROR,'\GuzzleHttp\Exception\BadResponseException',['message'=>$e->getMessage(),'path request'=>$e->getRequest()->getUri()->getPath(),'method'=>$e->getRequest()->getMethod(),'response status code'=>$responseStatusCode]); |
119
|
|
|
$this->setLog(LogLevel::DEBUG,'\GuzzleHttp\Exception\BadResponseException',['trace'=>$e->getTraceAsString()]); |
120
|
3 |
|
throw new \GuzzleHttp\Exception\BadResponseException($e->getMessage().' '.$responseStatusCode,$e->getRequest(),$e->getResponse(),$e,$e->getHandlerContext()); |
121
|
|
|
|
122
|
3 |
|
} catch (\GuzzleHttp\Exception\ConnectException $e) { |
123
|
3 |
|
|
124
|
3 |
|
$responseStatusCode = $this->getResponseStatusCodeFromException($e); |
125
|
3 |
|
$this->setLog(LogLevel::ERROR,'\GuzzleHttp\Exception\ConnectException',['message'=>$e->getMessage(),'path request'=>$e->getRequest()->getUri()->getPath(),'method'=>$e->getRequest()->getMethod(),'response status code'=>$responseStatusCode]); |
126
|
|
|
$this->setLog(LogLevel::DEBUG,'\GuzzleHttp\Exception\ConnectException',['trace'=>$e->getTraceAsString()]); |
127
|
3 |
|
throw new \GuzzleHttp\Exception\ConnectException($e->getMessage().' '.$responseStatusCode,$e->getRequest(),$e,$e->getHandlerContext()); |
128
|
|
|
|
129
|
3 |
|
} catch (\GuzzleHttp\Exception\TooManyRedirectsException $e) { |
130
|
3 |
|
|
131
|
3 |
|
$responseStatusCode = $this->getResponseStatusCodeFromException($e); |
132
|
3 |
|
$this->setLog(LogLevel::ERROR,'\GuzzleHttp\Exception\TooManyRedirectsException',['message'=>$e->getMessage(),'path request'=>$e->getRequest()->getUri()->getPath(),'method'=>$e->getRequest()->getMethod(),'response status code'=>$responseStatusCode]); |
133
|
|
|
$this->setLog(LogLevel::DEBUG,'\GuzzleHttp\Exception\TooManyRedirectsException',['trace'=>$e->getTraceAsString()]); |
134
|
3 |
|
throw new \GuzzleHttp\Exception\TooManyRedirectsException($e->getMessage().' '.$responseStatusCode,$e->getRequest(),$e->getResponse(),$e,$e->getHandlerContext()); |
135
|
|
|
|
136
|
3 |
|
} catch (\GuzzleHttp\Exception\RequestException $e) { |
137
|
3 |
|
|
138
|
3 |
|
$responseStatusCode = $this->getResponseStatusCodeFromException($e); |
139
|
3 |
|
$this->setLog(LogLevel::ERROR,'\GuzzleHttp\Exception\RequestException',['message'=>$e->getMessage(),'path request'=>$e->getRequest()->getUri()->getPath(),'method'=>$e->getRequest()->getMethod(),'response status code'=>$responseStatusCode]); |
140
|
|
|
$this->setLog(LogLevel::DEBUG,'\GuzzleHttp\Exception\RequestException',['trace'=>$e->getTraceAsString()]); |
141
|
3 |
|
throw new \GuzzleHttp\Exception\RequestException($e->getMessage().' '.$responseStatusCode,$e->getRequest(),$e->getResponse(),$e,$e->getHandlerContext()); |
142
|
|
|
|
143
|
3 |
|
} catch (\GuzzleHttp\Exception\TransferException $e) { |
144
|
3 |
|
|
145
|
3 |
|
$this->setLog(LogLevel::ERROR,'\GuzzleHttp\Exception\TransferException',['message'=>$e->getMessage(),'code'=>$e->getCode()]); |
146
|
|
|
$this->setLog(LogLevel::DEBUG,'\GuzzleHttp\Exception\TransferException',['trace'=>$e->getTraceAsString()]); |
147
|
3 |
|
throw new \GuzzleHttp\Exception\TransferException($e->getMessage(),$e->getCode(),$e); |
148
|
|
|
|
149
|
3 |
|
} catch (\RuntimeException $e) { |
150
|
3 |
|
|
151
|
3 |
|
$this->setLog(LogLevel::ERROR,'\RuntimeException',['message'=>$e->getMessage(),'code'=>$e->getCode()]); |
152
|
|
|
$this->setLog(LogLevel::DEBUG,'\RuntimeException',['trace'=>$e->getTraceAsString()]); |
153
|
|
|
throw new \RuntimeException($e->getMessage(),$e->getCode(),$e); |
154
|
|
|
|
155
|
9 |
|
} |
156
|
|
|
|
157
|
|
|
return $this->response; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $logLevel |
162
|
|
|
* @param string $message |
163
|
|
|
* @param null Array $context |
164
|
15 |
|
* @throws Exception |
165
|
|
|
*/ |
166
|
15 |
|
private function setLog($logLevel, $message, $context = null) |
167
|
15 |
|
{ |
168
|
|
|
if(!isset($this->log)){ |
169
|
|
|
return; |
170
|
|
|
} |
171
|
|
|
if(! is_array($context)){ |
172
|
|
|
throw new Exception('$context must be an array',0); |
173
|
|
|
} |
174
|
|
|
$this->log->log($logLevel, $message, $context); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param $property |
179
|
3 |
|
* @return mixed |
180
|
3 |
|
*/ |
181
|
3 |
|
public function __get($property) { |
182
|
|
|
if (property_exists($this, $property)) { |
183
|
|
|
return $this->$property; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param $property |
189
|
|
|
* @param $value |
190
|
|
|
*/ |
191
|
|
|
public function __set($property, $value) { |
192
|
|
|
if (property_exists($this, $property)) { |
193
|
|
|
$this->$property = $value; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
15 |
|
* Reset all variable to start new request. |
199
|
|
|
*/ |
200
|
15 |
|
public function reset() |
201
|
15 |
|
{ |
202
|
|
|
$this->response = new Response(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param \Exception $e |
207
|
6 |
|
* @return string |
208
|
|
|
*/ |
209
|
6 |
|
public function getResponseStatusCodeFromException(\Exception $e) |
210
|
3 |
|
{ |
211
|
|
|
if (null === $e->getResponse()) { |
|
|
|
|
212
|
6 |
|
return ''; |
213
|
|
|
} |
214
|
|
|
if (! is_a($e->getResponse(), '\GuzzleHttp\Psr7\Response')){ |
|
|
|
|
215
|
|
|
return ''; |
216
|
6 |
|
} |
217
|
6 |
|
|
218
|
|
|
$responseStatusCode = $e->getResponse()->getStatusCode(); |
|
|
|
|
219
|
|
|
return $responseStatusCode; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get psr7 headers array and return string representation. |
224
|
|
|
* @param \Psr\Http\Message\ResponseInterface |
225
|
9 |
|
* @return string |
226
|
|
|
*/ |
227
|
9 |
|
public function getHeadersString(\Psr\Http\Message\ResponseInterface $psr7response) |
228
|
9 |
|
{ |
229
|
9 |
|
$strHeaders = ''; |
230
|
|
|
if($psr7response === null) |
231
|
|
|
{ |
232
|
|
|
return $strHeaders; |
233
|
9 |
|
} |
234
|
9 |
|
|
235
|
6 |
|
$arrHeaders = $psr7response->getHeaders(); |
236
|
|
|
if (!is_array($arrHeaders) || count($arrHeaders) < 1) { |
237
|
|
|
return ''; |
238
|
3 |
|
} |
239
|
|
|
|
240
|
3 |
|
foreach ($arrHeaders as $key => $value) { |
241
|
3 |
|
|
242
|
3 |
|
$strVal = ''; |
243
|
|
|
if(is_array($value) && count($value)>0){ |
244
|
3 |
|
$strVal = $value[0]; |
245
|
|
|
|
246
|
|
|
}elseif (is_string($value)){ |
247
|
|
|
$strVal = $value; |
248
|
3 |
|
} |
249
|
3 |
|
|
250
|
|
|
$strHeaders .= $key.':'.$strVal.PHP_EOL; |
251
|
3 |
|
} |
252
|
|
|
|
253
|
|
|
return $strHeaders; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
|
258
|
|
|
|
259
|
|
|
} |
260
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.