OAuthCommonHttpClientStreamClient

Client implementation for streams/file_get_contents.

Defined (1)

The class is defined in the following location(s).

/lib/PHPoAuthLib/src/OAuth/Common/Http/Client/StreamClient.php  
  1. class StreamClient extends AbstractClient 
  2. /** 
  3. * Any implementing HTTP providers should send a request to the provided endpoint with the parameters. 
  4. * They should return, in string form, the response body and throw an exception on error. 
  5. * @param UriInterface $endpoint 
  6. * @param mixed $requestBody 
  7. * @param array $extraHeaders 
  8. * @param string $method 
  9. * @return string 
  10. * @throws TokenResponseException 
  11. * @throws \InvalidArgumentException 
  12. */ 
  13. public function retrieveResponse( 
  14. UriInterface $endpoint,  
  15. $requestBody,  
  16. array $extraHeaders = array(),  
  17. $method = 'POST' 
  18. ) { 
  19. // Normalize method name 
  20. $method = strtoupper($method); 
  21.  
  22. $this->normalizeHeaders($extraHeaders); 
  23.  
  24. if ($method === 'GET' && !empty($requestBody)) { 
  25. throw new \InvalidArgumentException('No body expected for "GET" request.'); 
  26.  
  27. if (!isset($extraHeaders['Content-Type']) && $method === 'POST' && is_array($requestBody)) { 
  28. $extraHeaders['Content-Type'] = 'Content-Type: application/x-www-form-urlencoded'; 
  29.  
  30. $host = 'Host: '.$endpoint->getHost(); 
  31. // Append port to Host if it has been specified 
  32. if ($endpoint->hasExplicitPortSpecified()) { 
  33. $host .= ':'.$endpoint->getPort(); 
  34.  
  35. $extraHeaders['Host'] = $host; 
  36. $extraHeaders['Connection'] = 'Connection: close'; 
  37.  
  38. if (is_array($requestBody)) { 
  39. $requestBody = http_build_query($requestBody, '', '&'); 
  40. $extraHeaders['Content-length'] = 'Content-length: '.strlen($requestBody); 
  41.  
  42. $context = $this->generateStreamContext($requestBody, $extraHeaders, $method); 
  43.  
  44. $level = error_reporting(0); 
  45. $response = file_get_contents($endpoint->getAbsoluteUri(), false, $context); 
  46. error_reporting($level); 
  47. if (false === $response) { 
  48. $lastError = error_get_last(); 
  49. if (is_null($lastError)) { 
  50. throw new TokenResponseException('Failed to request resource.'); 
  51. throw new TokenResponseException($lastError['message']); 
  52.  
  53. return $response; 
  54.  
  55. private function generateStreamContext($body, $headers, $method) 
  56. return stream_context_create( 
  57. array( 
  58. 'http' => array( 
  59. 'method' => $method,  
  60. 'header' => implode("\r\n", array_values($headers)),  
  61. 'content' => $body,  
  62. 'protocol_version' => '1.1',  
  63. 'user_agent' => $this->userAgent,  
  64. 'max_redirects' => $this->maxRedirects,  
  65. 'timeout' => $this->timeout 
  66. ),  
  67. );