Google_IO_Curl

The Gravity Forms Advanced File Uploader Google IO Curl class.

Defined (1)

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

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