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