PageRenderTime 46ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/PayPal/Core/PayPalHttpConnection.php

https://gitlab.com/CORP-RESELLER/PayPal-PHP-SDK
PHP | 193 lines | 114 code | 23 blank | 56 comment | 19 complexity | fd76e4f3fe077cde2a65bbdac8376398 MD5 | raw file
  1. <?php
  2. namespace PayPal\Core;
  3. use PayPal\Exception\PayPalConfigurationException;
  4. use PayPal\Exception\PayPalConnectionException;
  5. /**
  6. * A wrapper class based on the curl extension.
  7. * Requires the PHP curl module to be enabled.
  8. * See for full requirements the PHP manual: http://php.net/curl
  9. */
  10. class PayPalHttpConnection
  11. {
  12. /**
  13. * @var PayPalHttpConfig
  14. */
  15. private $httpConfig;
  16. /**
  17. * HTTP status codes for which a retry must be attempted
  18. * retry is currently attempted for Request timeout, Bad Gateway,
  19. * Service Unavailable and Gateway timeout errors.
  20. */
  21. private static $retryCodes = array('408', '502', '503', '504',);
  22. /**
  23. * LoggingManager
  24. *
  25. * @var PayPalLoggingManager
  26. */
  27. private $logger;
  28. /**
  29. * Default Constructor
  30. *
  31. * @param PayPalHttpConfig $httpConfig
  32. * @param array $config
  33. * @throws PayPalConfigurationException
  34. */
  35. public function __construct(PayPalHttpConfig $httpConfig, array $config)
  36. {
  37. if (!function_exists("curl_init")) {
  38. throw new PayPalConfigurationException("Curl module is not available on this system");
  39. }
  40. $this->httpConfig = $httpConfig;
  41. $this->logger = PayPalLoggingManager::getInstance(__CLASS__);
  42. }
  43. /**
  44. * Gets all Http Headers
  45. *
  46. * @return array
  47. */
  48. private function getHttpHeaders()
  49. {
  50. $ret = array();
  51. foreach ($this->httpConfig->getHeaders() as $k => $v) {
  52. $ret[] = "$k: $v";
  53. }
  54. return $ret;
  55. }
  56. /**
  57. * Executes an HTTP request
  58. *
  59. * @param string $data query string OR POST content as a string
  60. * @return mixed
  61. * @throws PayPalConnectionException
  62. */
  63. public function execute($data)
  64. {
  65. //Initialize the logger
  66. $this->logger->info($this->httpConfig->getMethod() . ' ' . $this->httpConfig->getUrl());
  67. //Initialize Curl Options
  68. $ch = curl_init($this->httpConfig->getUrl());
  69. $options = $this->httpConfig->getCurlOptions();
  70. if (empty($options[CURLOPT_HTTPHEADER])) {
  71. unset($options[CURLOPT_HTTPHEADER]);
  72. }
  73. curl_setopt_array($ch, $options);
  74. curl_setopt($ch, CURLOPT_URL, $this->httpConfig->getUrl());
  75. curl_setopt($ch, CURLOPT_HEADER, true);
  76. curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  77. curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHttpHeaders());
  78. //Determine Curl Options based on Method
  79. switch ($this->httpConfig->getMethod()) {
  80. case 'POST':
  81. curl_setopt($ch, CURLOPT_POST, true);
  82. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  83. break;
  84. case 'PUT':
  85. case 'PATCH':
  86. case 'DELETE':
  87. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  88. break;
  89. }
  90. //Default Option if Method not of given types in switch case
  91. if ($this->httpConfig->getMethod() != null) {
  92. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->httpConfig->getMethod());
  93. }
  94. //Logging Each Headers for debugging purposes
  95. foreach ($this->getHttpHeaders() as $header) {
  96. //TODO: Strip out credentials and other secure info when logging.
  97. // $this->logger->debug($header);
  98. }
  99. //Execute Curl Request
  100. $result = curl_exec($ch);
  101. //Retrieve Response Status
  102. $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  103. //Retry if Certificate Exception
  104. if (curl_errno($ch) == 60) {
  105. $this->logger->info("Invalid or no certificate authority found - Retrying using bundled CA certs file");
  106. curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
  107. $result = curl_exec($ch);
  108. //Retrieve Response Status
  109. $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  110. }
  111. //Retry if Failing
  112. $retries = 0;
  113. if (in_array($httpStatus, self::$retryCodes) && $this->httpConfig->getHttpRetryCount() != null) {
  114. $this->logger->info("Got $httpStatus response from server. Retrying");
  115. do {
  116. $result = curl_exec($ch);
  117. //Retrieve Response Status
  118. $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  119. } while (in_array($httpStatus, self::$retryCodes) && (++$retries < $this->httpConfig->getHttpRetryCount()));
  120. }
  121. //Throw Exception if Retries and Certificates doenst work
  122. if (curl_errno($ch)) {
  123. $ex = new PayPalConnectionException(
  124. $this->httpConfig->getUrl(),
  125. curl_error($ch),
  126. curl_errno($ch)
  127. );
  128. curl_close($ch);
  129. throw $ex;
  130. }
  131. // Get Request and Response Headers
  132. $requestHeaders = curl_getinfo($ch, CURLINFO_HEADER_OUT);
  133. //Using alternative solution to CURLINFO_HEADER_SIZE as it throws invalid number when called using PROXY.
  134. $responseHeaderSize = strlen($result) - curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD);
  135. $responseHeaders = substr($result, 0, $responseHeaderSize);
  136. $result = substr($result, $responseHeaderSize);
  137. $this->logger->debug("Request Headers \t: " . str_replace("\r\n", ", ", $requestHeaders));
  138. $this->logger->debug(($data && $data != '' ? "Request Data\t\t: " . $data : "No Request Payload") . "\n" . str_repeat('-', 128) . "\n");
  139. $this->logger->info("Response Status \t: " . $httpStatus);
  140. $this->logger->debug("Response Headers\t: " . str_replace("\r\n", ", ", $responseHeaders));
  141. //Close the curl request
  142. curl_close($ch);
  143. //More Exceptions based on HttpStatus Code
  144. if (in_array($httpStatus, self::$retryCodes)) {
  145. $ex = new PayPalConnectionException(
  146. $this->httpConfig->getUrl(),
  147. "Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}. " .
  148. "Retried $retries times."
  149. );
  150. $ex->setData($result);
  151. $this->logger->error("Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}. " .
  152. "Retried $retries times." . $result);
  153. $this->logger->debug("\n\n" . str_repeat('=', 128) . "\n");
  154. throw $ex;
  155. } elseif ($httpStatus < 200 || $httpStatus >= 300) {
  156. $ex = new PayPalConnectionException(
  157. $this->httpConfig->getUrl(),
  158. "Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}.",
  159. $httpStatus
  160. );
  161. $ex->setData($result);
  162. $this->logger->error("Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}. " . $result);
  163. $this->logger->debug("\n\n" . str_repeat('=', 128) . "\n");
  164. throw $ex;
  165. }
  166. $this->logger->debug(($result && $result != '' ? "Response Data \t: " . $result : "No Response Body") . "\n\n" . str_repeat('=', 128) . "\n");
  167. //Return result object
  168. return $result;
  169. }
  170. }