| 1 |
<?php |
| 2 |
/*! |
| 3 |
* Hybridauth |
| 4 |
* https://hybridauth.github.io | https://github.com/hybridauth/hybridauth |
| 5 |
* (c) 2017 Hybridauth authors | https://hybridauth.github.io/license.html |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Hybridauth\HttpClient; |
| 9 |
|
| 10 |
use GuzzleHttp\Client; |
| 11 |
use GuzzleHttp\Exception\RequestException; |
| 12 |
use GuzzleHttp\Exception\ServerException; |
| 13 |
use GuzzleHttp\Exception\TransferException; |
| 14 |
|
| 15 |
/** |
| 16 |
* Hybridauth Guzzle Http client |
| 17 |
* |
| 18 |
* Note: This is just a proof of concept. Feel free to improve it. |
| 19 |
* |
| 20 |
* Example: |
| 21 |
* |
| 22 |
* <code> |
| 23 |
* $guzzle = new Hybridauth\HttpClient\Guzzle(new GuzzleHttp\Client(), [ |
| 24 |
* 'verify' => '/path/to/your/certificate.crt', |
| 25 |
* 'headers' => ['User-Agent' => '..'] |
| 26 |
* // 'proxy' => ... |
| 27 |
* ]); |
| 28 |
* |
| 29 |
* $adapter = new Hybridauth\Provider\Github($config, $guzzle); |
| 30 |
* |
| 31 |
* $adapter->authenticate(); |
| 32 |
* </code> |
| 33 |
*/ |
| 34 |
class Guzzle implements HttpClientInterface |
| 35 |
{ |
| 36 |
/** |
| 37 |
* Method request() arguments |
| 38 |
* |
| 39 |
* This is used for debugging. |
| 40 |
* |
| 41 |
* @var array |
| 42 |
*/ |
| 43 |
protected $requestArguments = []; |
| 44 |
|
| 45 |
/** |
| 46 |
* Default request headers |
| 47 |
* |
| 48 |
* @var array |
| 49 |
*/ |
| 50 |
protected $requestHeader = []; |
| 51 |
|
| 52 |
/** |
| 53 |
* Raw response returned by server |
| 54 |
* |
| 55 |
* @var string |
| 56 |
*/ |
| 57 |
protected $responseBody = ''; |
| 58 |
|
| 59 |
/** |
| 60 |
* Headers returned in the response |
| 61 |
* |
| 62 |
* @var array |
| 63 |
*/ |
| 64 |
protected $responseHeader = []; |
| 65 |
|
| 66 |
/** |
| 67 |
* Response HTTP status code |
| 68 |
* |
| 69 |
* @var int |
| 70 |
*/ |
| 71 |
protected $responseHttpCode = 0; |
| 72 |
|
| 73 |
/** |
| 74 |
* Last curl error number |
| 75 |
* |
| 76 |
* @var mixed |
| 77 |
*/ |
| 78 |
protected $responseClientError = null; |
| 79 |
|
| 80 |
/** |
| 81 |
* Information about the last transfer |
| 82 |
* |
| 83 |
* @var mixed |
| 84 |
*/ |
| 85 |
protected $responseClientInfo = []; |
| 86 |
|
| 87 |
/** |
| 88 |
* Hybridauth logger instance |
| 89 |
* |
| 90 |
* @var object |
| 91 |
*/ |
| 92 |
protected $logger = null; |
| 93 |
|
| 94 |
/** |
| 95 |
* GuzzleHttp client |
| 96 |
* |
| 97 |
* @var \GuzzleHttp\Client |
| 98 |
*/ |
| 99 |
protected $client = null; |
| 100 |
|
| 101 |
/** |
| 102 |
* .. |
| 103 |
* @param null $client |
| 104 |
* @param array $config |
| 105 |
*/ |
| 106 |
public function __construct($client = null, $config = []) |
| 107 |
{ |
| 108 |
$this->client = $client ? $client : new Client($config); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* {@inheritdoc} |
| 113 |
*/ |
| 114 |
public function request($uri, $method = 'GET', $parameters = [], $headers = [], $multipart = false) |
| 115 |
{ |
| 116 |
$this->requestHeader = array_replace($this->requestHeader, (array)$headers); |
| 117 |
|
| 118 |
$this->requestArguments = [ |
| 119 |
'uri' => $uri, |
| 120 |
'method' => $method, |
| 121 |
'parameters' => $parameters, |
| 122 |
'headers' => $this->requestHeader, |
| 123 |
]; |
| 124 |
|
| 125 |
$response = null; |
| 126 |
|
| 127 |
try { |
| 128 |
switch ($method) { |
| 129 |
case 'GET': |
| 130 |
case 'DELETE': |
| 131 |
$response = $this->client->request($method, $uri, [ |
| 132 |
'query' => $parameters, |
| 133 |
'headers' => $this->requestHeader, |
| 134 |
]); |
| 135 |
break; |
| 136 |
case 'PUT': |
| 137 |
case 'PATCH': |
| 138 |
case 'POST': |
| 139 |
$body_type = $multipart ? 'multipart' : 'form_params'; |
| 140 |
|
| 141 |
if (isset($this->requestHeader['Content-Type']) |
| 142 |
&& $this->requestHeader['Content-Type'] === 'application/json' |
| 143 |
) { |
| 144 |
$body_type = 'json'; |
| 145 |
} |
| 146 |
|
| 147 |
$body_content = $parameters; |
| 148 |
if ($multipart) { |
| 149 |
$body_content = []; |
| 150 |
foreach ($parameters as $key => $val) { |
| 151 |
if ($val instanceof \CURLFile) { |
| 152 |
$val = fopen($val->getFilename(), 'r'); |
| 153 |
} |
| 154 |
|
| 155 |
$body_content[] = [ |
| 156 |
'name' => $key, |
| 157 |
'contents' => $val, |
| 158 |
]; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
$response = $this->client->request($method, $uri, [ |
| 163 |
$body_type => $body_content, |
| 164 |
'headers' => $this->requestHeader, |
| 165 |
]); |
| 166 |
break; |
| 167 |
} |
| 168 |
} catch (\Exception $e) { |
| 169 |
$response = $e->getResponse(); |
| 170 |
|
| 171 |
$this->responseClientError = $e->getMessage(); |
| 172 |
} |
| 173 |
|
| 174 |
if (!$this->responseClientError) { |
| 175 |
$this->responseBody = $response->getBody(); |
| 176 |
$this->responseHttpCode = $response->getStatusCode(); |
| 177 |
$this->responseHeader = $response->getHeaders(); |
| 178 |
} |
| 179 |
|
| 180 |
if ($this->logger) { |
| 181 |
// phpcs:ignore |
| 182 |
$this->logger->debug(sprintf('%s::request( %s, %s ), response:', get_class($this), $uri, $method), $this->getResponse()); |
| 183 |
|
| 184 |
if ($this->responseClientError) { |
| 185 |
// phpcs:ignore |
| 186 |
$this->logger->error(sprintf('%s::request( %s, %s ), error:', get_class($this), $uri, $method), [$this->responseClientError]); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
return $this->responseBody; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Get response details |
| 195 |
* |
| 196 |
* @return array Map structure of details |
| 197 |
*/ |
| 198 |
public function getResponse() |
| 199 |
{ |
| 200 |
return [ |
| 201 |
'request' => $this->getRequestArguments(), |
| 202 |
'response' => [ |
| 203 |
'code' => $this->getResponseHttpCode(), |
| 204 |
'headers' => $this->getResponseHeader(), |
| 205 |
'body' => $this->getResponseBody(), |
| 206 |
], |
| 207 |
'client' => [ |
| 208 |
'error' => $this->getResponseClientError(), |
| 209 |
'info' => $this->getResponseClientInfo(), |
| 210 |
'opts' => null, |
| 211 |
], |
| 212 |
]; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Set logger instance |
| 217 |
* |
| 218 |
* @param object $logger |
| 219 |
*/ |
| 220 |
public function setLogger($logger) |
| 221 |
{ |
| 222 |
$this->logger = $logger; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* {@inheritdoc} |
| 227 |
*/ |
| 228 |
public function getResponseBody() |
| 229 |
{ |
| 230 |
return $this->responseBody; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* {@inheritdoc} |
| 235 |
*/ |
| 236 |
public function getResponseHeader() |
| 237 |
{ |
| 238 |
return $this->responseHeader; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* {@inheritdoc} |
| 243 |
*/ |
| 244 |
public function getResponseHttpCode() |
| 245 |
{ |
| 246 |
return $this->responseHttpCode; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* {@inheritdoc} |
| 251 |
*/ |
| 252 |
public function getResponseClientError() |
| 253 |
{ |
| 254 |
return $this->responseClientError; |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* @return array |
| 259 |
*/ |
| 260 |
protected function getResponseClientInfo() |
| 261 |
{ |
| 262 |
return $this->responseClientInfo; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Returns method request() arguments |
| 267 |
* |
| 268 |
* This is used for debugging. |
| 269 |
* |
| 270 |
* @return array |
| 271 |
*/ |
| 272 |
protected function getRequestArguments() |
| 273 |
{ |
| 274 |
return $this->requestArguments; |
| 275 |
} |
| 276 |
} |
| 277 |
|