|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OAuth\Common\Http\Client; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use OAuth\Common\Http\Exception\TokenResponseException; |
|
7
|
|
|
use OAuth\Common\Http\Uri\UriInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Client implementation for cURL. |
|
11
|
|
|
*/ |
|
12
|
|
|
class CurlClient extends AbstractClient |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* If true, explicitly sets cURL to use SSL version 3. Use this if cURL |
|
16
|
|
|
* compiles with GnuTLS SSL. |
|
17
|
|
|
* |
|
18
|
|
|
* @var bool |
|
19
|
|
|
*/ |
|
20
|
|
|
private $forceSSL3 = false; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Additional parameters (as `key => value` pairs) to be passed to `curl_setopt`. |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
private $parameters = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Additional `curl_setopt` parameters. |
|
31
|
|
|
*/ |
|
32
|
|
|
public function setCurlParameters(array $parameters): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->parameters = $parameters; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param bool $force |
|
39
|
|
|
* |
|
40
|
|
|
* @return CurlClient |
|
41
|
|
|
*/ |
|
42
|
|
|
public function setForceSSL3($force) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->forceSSL3 = $force; |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Any implementing HTTP providers should send a request to the provided endpoint with the parameters. |
|
51
|
|
|
* They should return, in string form, the response body and throw an exception on error. |
|
52
|
|
|
* |
|
53
|
|
|
* @param mixed $requestBody |
|
54
|
|
|
* @param string $method |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function retrieveResponse( |
|
59
|
|
|
UriInterface $endpoint, |
|
60
|
|
|
$requestBody, |
|
61
|
|
|
array $extraHeaders = [], |
|
62
|
|
|
$method = 'POST' |
|
63
|
|
|
) { |
|
64
|
|
|
// Normalize method name |
|
65
|
|
|
$method = strtoupper($method); |
|
66
|
|
|
|
|
67
|
|
|
$extraHeaders = $this->normalizeHeaders($extraHeaders); |
|
68
|
|
|
|
|
69
|
|
|
if ($method === 'GET' && !empty($requestBody)) { |
|
70
|
|
|
throw new InvalidArgumentException('No body expected for "GET" request.'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (!isset($extraHeaders['Content-Type']) && $method === 'POST' && is_array($requestBody)) { |
|
74
|
|
|
$extraHeaders['Content-Type'] = 'Content-Type: application/x-www-form-urlencoded'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$extraHeaders['Host'] = 'Host: ' . $endpoint->getHost(); |
|
78
|
|
|
$extraHeaders['Connection'] = 'Connection: close'; |
|
79
|
|
|
|
|
80
|
|
|
$ch = curl_init(); |
|
81
|
|
|
|
|
82
|
|
|
curl_setopt($ch, CURLOPT_URL, $endpoint->getAbsoluteUri()); |
|
83
|
|
|
|
|
84
|
|
|
if ($method === 'POST' || $method === 'PUT') { |
|
85
|
|
|
if ($requestBody && is_array($requestBody)) { |
|
86
|
|
|
$requestBody = http_build_query($requestBody, '', '&'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ($method === 'PUT') { |
|
90
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
|
91
|
|
|
} else { |
|
92
|
|
|
curl_setopt($ch, CURLOPT_POST, true); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); |
|
96
|
|
|
} else { |
|
97
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ($this->maxRedirects > 0) { |
|
101
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
|
102
|
|
|
curl_setopt($ch, CURLOPT_MAXREDIRS, $this->maxRedirects); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); |
|
106
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
107
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false); |
|
108
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $extraHeaders); |
|
109
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); |
|
110
|
|
|
|
|
111
|
|
|
foreach ($this->parameters as $key => $value) { |
|
112
|
|
|
curl_setopt($ch, $key, $value); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$response = curl_exec($ch); |
|
116
|
|
|
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
117
|
|
|
|
|
118
|
|
|
if (false === $response) { |
|
119
|
|
|
$errNo = curl_errno($ch); |
|
120
|
|
|
$errStr = curl_error($ch); |
|
121
|
|
|
curl_close($ch); |
|
122
|
|
|
if (empty($errStr)) { |
|
123
|
|
|
throw new TokenResponseException('Failed to request resource.', $responseCode); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
throw new TokenResponseException('cURL Error # ' . $errNo . ': ' . $errStr, $responseCode); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
curl_close($ch); |
|
130
|
|
|
|
|
131
|
|
|
return $response; |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|