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 streams/file_get_contents. |
11
|
|
|
*/ |
12
|
|
|
class StreamClient extends AbstractClient |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Any implementing HTTP providers should send a request to the provided endpoint with the parameters. |
16
|
|
|
* They should return, in string form, the response body and throw an exception on error. |
17
|
|
|
* |
18
|
|
|
* @param mixed $requestBody |
19
|
|
|
* @param string $method |
20
|
|
|
* |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
public function retrieveResponse( |
24
|
|
|
UriInterface $endpoint, |
25
|
|
|
$requestBody, |
26
|
|
|
array $extraHeaders = [], |
27
|
|
|
$method = 'POST' |
28
|
|
|
) { |
29
|
|
|
// Normalize method name |
30
|
|
|
$method = strtoupper($method); |
31
|
|
|
|
32
|
|
|
$extraHeaders = $this->normalizeHeaders($extraHeaders); |
33
|
|
|
|
34
|
|
|
if ($method === 'GET' && !empty($requestBody)) { |
35
|
|
|
throw new InvalidArgumentException('No body expected for "GET" request.'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (!isset($extraHeaders['Content-Type']) && $method === 'POST' && is_array($requestBody)) { |
39
|
|
|
$extraHeaders['Content-Type'] = 'Content-Type: application/x-www-form-urlencoded'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$host = 'Host: ' . $endpoint->getHost(); |
43
|
|
|
// Append port to Host if it has been specified |
44
|
|
|
if ($endpoint->hasExplicitPortSpecified()) { |
45
|
|
|
$host .= ':' . $endpoint->getPort(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$extraHeaders['Host'] = $host; |
49
|
|
|
$extraHeaders['Connection'] = 'Connection: close'; |
50
|
|
|
|
51
|
|
|
if (is_array($requestBody)) { |
52
|
|
|
$requestBody = http_build_query($requestBody, '', '&'); |
53
|
|
|
} |
54
|
|
|
$extraHeaders['Content-length'] = 'Content-length: ' . strlen($requestBody); |
55
|
|
|
|
56
|
|
|
$context = $this->generateStreamContext($requestBody, $extraHeaders, $method); |
57
|
|
|
|
58
|
|
|
$level = error_reporting(0); |
59
|
|
|
$response = file_get_contents($endpoint->getAbsoluteUri(), false, $context); |
60
|
|
|
error_reporting($level); |
61
|
|
|
if (false === $response) { |
62
|
|
|
$lastError = error_get_last(); |
63
|
|
|
if (null === $lastError) { |
64
|
|
|
throw new TokenResponseException( |
65
|
|
|
'Failed to request resource. HTTP Code: ' . |
66
|
|
|
((isset($http_response_header[0])) ? $http_response_header[0] : 'No response') |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
throw new TokenResponseException($lastError['message']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $response; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function generateStreamContext($body, $headers, $method) |
77
|
|
|
{ |
78
|
|
|
return stream_context_create( |
79
|
|
|
[ |
80
|
|
|
'http' => [ |
81
|
|
|
'method' => $method, |
82
|
|
|
'header' => implode("\r\n", array_values($headers)), |
83
|
|
|
'content' => $body, |
84
|
|
|
'protocol_version' => '1.1', |
85
|
|
|
'user_agent' => $this->userAgent, |
86
|
|
|
'max_redirects' => $this->maxRedirects, |
87
|
|
|
'timeout' => $this->timeout, |
88
|
|
|
], |
89
|
|
|
] |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|