Google_IO_Curl

The Google Photos & Picasa Viewer Google IO Curl class.

Defined (2)

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

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