Google_IO_Curl

The Simple Calendar Google IO Curl class.

Defined (1)

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

/vendor/google/apiclient/src/Google/IO/Curl.php  
  1. class Google_IO_Curl extends Google_IO_Abstract 
  2. // cURL hex representation of version 7.30.0 
  3. const NO_QUIRK_VERSION = 0x071E00; 
  4.  
  5. private $options = array(); 
  6.  
  7. /** @var bool $disableProxyWorkaround */ 
  8. private $disableProxyWorkaround; 
  9.  
  10. public function __construct(Google_Client $client) 
  11. if (!extension_loaded('curl')) { 
  12. $error = 'The cURL IO handler requires the cURL extension to be enabled'; 
  13. $client->getLogger()->critical($error); 
  14. throw new Google_IO_Exception($error); 
  15.  
  16. parent::__construct($client); 
  17.  
  18. $this->disableProxyWorkaround = $this->client->getClassConfig( 
  19. 'Google_IO_Curl',  
  20. 'disable_proxy_workaround' 
  21. ); 
  22.  
  23. /** 
  24. * Execute an HTTP Request 
  25. * @param Google_Http_Request $request the http request to be executed 
  26. * @return array containing response headers, body, and http code 
  27. * @throws Google_IO_Exception on curl or IO error 
  28. */ 
  29. public function executeRequest(Google_Http_Request $request) 
  30. $curl = curl_init(); 
  31.  
  32. if ($request->getPostBody()) { 
  33. curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody()); 
  34.  
  35. $requestHeaders = $request->getRequestHeaders(); 
  36. if ($requestHeaders && is_array($requestHeaders)) { 
  37. $curlHeaders = array(); 
  38. foreach ($requestHeaders as $k => $v) { 
  39. $curlHeaders[] = "$k: $v"; 
  40. curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders); 
  41. curl_setopt($curl, CURLOPT_URL, $request->getUrl()); 
  42.  
  43. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod()); 
  44. curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent()); 
  45.  
  46. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); 
  47. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); 
  48.  
  49. // The SSL version will be determined by the underlying library 
  50. // @see https://github.com/google/google-api-php-client/pull/644 
  51. //curl_setopt($curl, CURLOPT_SSLVERSION, 1); 
  52.  
  53. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
  54. curl_setopt($curl, CURLOPT_HEADER, true); 
  55.  
  56. if ($request->canGzip()) { 
  57. curl_setopt($curl, CURLOPT_ENCODING, 'gzip, deflate'); 
  58.  
  59. $options = $this->client->getClassConfig('Google_IO_Curl', 'options'); 
  60. if (is_array($options)) { 
  61. $this->setOptions($options); 
  62.  
  63. foreach ($this->options as $key => $var) { 
  64. curl_setopt($curl, $key, $var); 
  65.  
  66. if (!isset($this->options[CURLOPT_CAINFO])) { 
  67. curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem'); 
  68.  
  69. $this->client->getLogger()->debug( 
  70. 'cURL request',  
  71. array( 
  72. 'url' => $request->getUrl(),  
  73. 'method' => $request->getRequestMethod(),  
  74. 'headers' => $requestHeaders,  
  75. 'body' => $request->getPostBody() 
  76. ); 
  77.  
  78. $response = curl_exec($curl); 
  79. if ($response === false) { 
  80. $error = curl_error($curl); 
  81. $code = curl_errno($curl); 
  82. $map = $this->client->getClassConfig('Google_IO_Exception', 'retry_map'); 
  83.  
  84. $this->client->getLogger()->error('cURL ' . $error); 
  85. throw new Google_IO_Exception($error, $code, null, $map); 
  86. $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); 
  87.  
  88. list($responseHeaders, $responseBody) = $this->parseHttpResponse($response, $headerSize); 
  89. $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
  90.  
  91. $this->client->getLogger()->debug( 
  92. 'cURL response',  
  93. array( 
  94. 'code' => $responseCode,  
  95. 'headers' => $responseHeaders,  
  96. 'body' => $responseBody,  
  97. ); 
  98.  
  99. return array($responseBody, $responseHeaders, $responseCode); 
  100.  
  101. /** 
  102. * Set options that update the transport implementation's behavior. 
  103. * @param $options 
  104. */ 
  105. public function setOptions($options) 
  106. $this->options = $options + $this->options; 
  107.  
  108. /** 
  109. * Set the maximum request time in seconds. 
  110. * @param $timeout in seconds 
  111. */ 
  112. public function setTimeout($timeout) 
  113. // Since this timeout is really for putting a bound on the time 
  114. // we'll set them both to the same. If you need to specify a longer 
  115. // CURLOPT_TIMEOUT, or a higher CONNECTTIMEOUT, the best thing to 
  116. // do is use the setOptions method for the values individually. 
  117. $this->options[CURLOPT_CONNECTTIMEOUT] = $timeout; 
  118. $this->options[CURLOPT_TIMEOUT] = $timeout; 
  119.  
  120. /** 
  121. * Get the maximum request time in seconds. 
  122. * @return timeout in seconds 
  123. */ 
  124. public function getTimeout() 
  125. return $this->options[CURLOPT_TIMEOUT]; 
  126.  
  127. /** 
  128. * Test for the presence of a cURL header processing bug 
  129. * {@inheritDoc} 
  130. * @return boolean 
  131. */ 
  132. protected function needsQuirk() 
  133. if ($this->disableProxyWorkaround) { 
  134. return false; 
  135.  
  136. $ver = curl_version(); 
  137. $versionNum = $ver['version_number']; 
  138. return $versionNum < Google_IO_Curl::NO_QUIRK_VERSION;