StreamClient.php 3.3 KB

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