Google_IO_Curl

The WordPress Core Google IO Curl class.

Defined (1)

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

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