OAuthCommonHttpClientCurlClient

Client implementation for cURL.

Defined (1)

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

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