Google_CurlIO

The BestWebSoft Google Analytics Google CurlIO class.

Defined (1)

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

/google-api-php-client/api-code/io/Google_CurlIO.php  
  1. class Google_CurlIO extends Google_IO { 
  2. private static $ENTITY_HTTP_METHODS = array("POST" => null, "PUT" => null); 
  3. private static $HOP_BY_HOP = array( 
  4. 'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization',  
  5. 'te', 'trailers', 'transfer-encoding', 'upgrade'); 
  6.  
  7. private $curlParams = array ( 
  8. CURLOPT_RETURNTRANSFER => true,  
  9. CURLOPT_FOLLOWLOCATION => 0,  
  10. CURLOPT_FAILONERROR => false,  
  11. CURLOPT_SSL_VERIFYPEER => true,  
  12. CURLOPT_HEADER => true,  
  13. CURLOPT_VERBOSE => false,  
  14. ); 
  15.  
  16. /** 
  17. * Check for cURL availability. 
  18. */ 
  19. public function __construct() { 
  20. if (! function_exists('curl_init')) { 
  21. throw new Exception( 
  22. 'Google CurlIO client requires the CURL PHP extension'); 
  23.  
  24. /** 
  25. * Perform an authenticated / signed apiHttpRequest. 
  26. * This function takes the apiHttpRequest, calls apiAuth->sign on it 
  27. * (which can modify the request in what ever way fits the auth mechanism) 
  28. * and then calls apiCurlIO::makeRequest on the signed request 
  29. * @param Google_HttpRequest $request 
  30. * @return Google_HttpRequest The resulting HTTP response including the 
  31. * responseHttpCode, responseHeaders and responseBody. 
  32. */ 
  33. public function authenticatedRequest(Google_HttpRequest $request) { 
  34. $request = Google_Client::$auth->sign($request); 
  35. return $this->makeRequest($request); 
  36.  
  37. /** 
  38. * Execute a apiHttpRequest 
  39. * @param Google_HttpRequest $request the http request to be executed 
  40. * @return Google_HttpRequest http request with the response http code, response 
  41. * headers and response body filled in 
  42. * @throws Google_IOException on curl or IO error 
  43. */ 
  44. public function makeRequest(Google_HttpRequest $request) { 
  45. // First, check to see if we have a valid cached version. 
  46. $cached = $this->getCachedRequest($request); 
  47. if ($cached !== false) { 
  48. if (!$this->checkMustRevaliadateCachedRequest($cached, $request)) { 
  49. return $cached; 
  50.  
  51. if (array_key_exists($request->getRequestMethod(),  
  52. self::$ENTITY_HTTP_METHODS)) { 
  53. $request = $this->processEntityRequest($request); 
  54.  
  55. $ch = curl_init(); 
  56. curl_setopt_array($ch, $this->curlParams); 
  57. curl_setopt($ch, CURLOPT_URL, $request->getUrl()); 
  58. if ($request->getPostBody()) { 
  59. curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getPostBody()); 
  60.  
  61. $requestHeaders = $request->getRequestHeaders(); 
  62. if ($requestHeaders && is_array($requestHeaders)) { 
  63. $parsed = array(); 
  64. foreach ($requestHeaders as $k => $v) { 
  65. $parsed[] = "$k: $v"; 
  66. curl_setopt($ch, CURLOPT_HTTPHEADER, $parsed); 
  67.  
  68. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod()); 
  69. curl_setopt($ch, CURLOPT_USERAGENT, $request->getUserAgent()); 
  70. $respData = curl_exec($ch); 
  71.  
  72. // Retry if certificates are missing. 
  73. if (curl_errno($ch) == CURLE_SSL_CACERT) { 
  74. error_log('SSL certificate problem, verify that the CA cert is OK.' 
  75. . ' Retrying with the CA cert bundle from google-api-php-client.'); 
  76. curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem'); 
  77. $respData = curl_exec($ch); 
  78.  
  79. $respHeaderSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); 
  80. $respHttpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); 
  81. $curlErrorNum = curl_errno($ch); 
  82. $curlError = curl_error($ch); 
  83. curl_close($ch); 
  84. if ($curlErrorNum != CURLE_OK) { 
  85. throw new Google_IOException("HTTP Error: ($respHttpCode) $curlError"); 
  86.  
  87. // Parse out the raw response into usable bits 
  88. list($responseHeaders, $responseBody) = 
  89. self::parseHttpResponse($respData, $respHeaderSize); 
  90.  
  91. if ($respHttpCode == 304 && $cached) { 
  92. // If the server responded NOT_MODIFIED, return the cached request. 
  93. $this->updateCachedRequest($cached, $responseHeaders); 
  94. return $cached; 
  95.  
  96. // Fill in the apiHttpRequest with the response values 
  97. $request->setResponseHttpCode($respHttpCode); 
  98. $request->setResponseHeaders($responseHeaders); 
  99. $request->setResponseBody($responseBody); 
  100. // Store the request in cache (the function checks to see if the request 
  101. // can actually be cached) 
  102. $this->setCachedRequest($request); 
  103. // And finally return it 
  104. return $request; 
  105.  
  106. /** 
  107. * Set options that update cURL's default behavior. 
  108. * The list of accepted options are: 
  109. * {@link http://php.net/manual/en/function.curl-setopt.php] 
  110. * @param array $optCurlParams Multiple options used by a cURL session. 
  111. */ 
  112. public function setOptions($optCurlParams) { 
  113. foreach ($optCurlParams as $key => $val) { 
  114. $this->curlParams[$key] = $val; 
  115.  
  116. /** 
  117. * @param $respData 
  118. * @param $headerSize 
  119. * @return array 
  120. */ 
  121. private static function parseHttpResponse($respData, $headerSize) { 
  122. if (stripos($respData, parent::CONNECTION_ESTABLISHED) !== false) { 
  123. $respData = str_ireplace(parent::CONNECTION_ESTABLISHED, '', $respData); 
  124.  
  125. if ($headerSize) { 
  126. $responseBody = substr($respData, $headerSize); 
  127. $responseHeaders = substr($respData, 0, $headerSize); 
  128. } else { 
  129. list($responseHeaders, $responseBody) = explode("\r\n\r\n", $respData, 2); 
  130.  
  131. $responseHeaders = self::parseResponseHeaders($responseHeaders); 
  132. return array($responseHeaders, $responseBody); 
  133.  
  134. private static function parseResponseHeaders($rawHeaders) { 
  135. $responseHeaders = array(); 
  136.  
  137. $responseHeaderLines = explode("\r\n", $rawHeaders); 
  138. foreach ($responseHeaderLines as $headerLine) { 
  139. if ($headerLine && strpos($headerLine, ':') !== false) { 
  140. list($header, $value) = explode(': ', $headerLine, 2); 
  141. $header = strtolower($header); 
  142. if (isset($responseHeaders[$header])) { 
  143. $responseHeaders[$header] .= "\n" . $value; 
  144. } else { 
  145. $responseHeaders[$header] = $value; 
  146. return $responseHeaders;