Google_IO_Curl

The SZ - Google Google IO Curl class.

Defined (1)

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

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