PayPalCorePayPalHttpConfig

Class PayPalHttpConfig Http Configuration Class.

Defined (1)

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

/classes/lib/PayPal/Core/PayPalHttpConfig.php  
  1. class PayPalHttpConfig 
  2. /** 
  3. * Some default options for curl 
  4. * These are typically overridden by PayPalConnectionManager 
  5. * @var array 
  6. */ 
  7. public static $defaultCurlOptions = array( 
  8. CURLOPT_SSLVERSION => 6,  
  9. CURLOPT_CONNECTTIMEOUT => 10,  
  10. CURLOPT_RETURNTRANSFER => TRUE,  
  11. CURLOPT_TIMEOUT => 60, // maximum number of seconds to allow cURL functions to execute 
  12. CURLOPT_USERAGENT => 'PayPal-PHP-SDK',  
  13. CURLOPT_HTTPHEADER => array(),  
  14. CURLOPT_SSL_VERIFYHOST => 2,  
  15. CURLOPT_SSL_VERIFYPEER => 1,  
  16. CURLOPT_SSL_CIPHER_LIST => 'TLSv1' 
  17. //Allowing TLSv1 cipher list. 
  18. //Adding it like this for backward compatibility with older versions of curl 
  19. ); 
  20.  
  21. const HEADER_SEPARATOR = ';'; 
  22. const HTTP_GET = 'GET'; 
  23. const HTTP_POST = 'POST'; 
  24.  
  25. private $headers = array(); 
  26.  
  27. private $curlOptions; 
  28.  
  29. private $url; 
  30.  
  31. private $method; 
  32.  
  33. /*** 
  34. * Number of times to retry a failed HTTP call 
  35. */ 
  36. private $retryCount = 0; 
  37.  
  38. /** 
  39. * Default Constructor 
  40. * @param string $url 
  41. * @param string $method HTTP method (GET, POST etc) defaults to POST 
  42. * @param array $configs All Configurations 
  43. */ 
  44. public function __construct($url = null, $method = self::HTTP_POST, $configs = array()) 
  45. $this->url = $url; 
  46. $this->method = $method; 
  47. $this->curlOptions = $this->getHttpConstantsFromConfigs($configs, 'http.') + self::$defaultCurlOptions; 
  48. // Update the Cipher List based on OpenSSL or NSS settings 
  49. $curl = curl_version(); 
  50. $sslVersion = isset($curl['ssl_version']) ? $curl['ssl_version'] : ''; 
  51. if (substr_compare($sslVersion, "NSS/", 0, strlen("NSS/")) === 0) { 
  52. //Remove the Cipher List for NSS 
  53. $this->removeCurlOption(CURLOPT_SSL_CIPHER_LIST); 
  54.  
  55. /** 
  56. * Gets Url 
  57. * @return null|string 
  58. */ 
  59. public function getUrl() 
  60. return $this->url; 
  61.  
  62. /** 
  63. * Gets Method 
  64. * @return string 
  65. */ 
  66. public function getMethod() 
  67. return $this->method; 
  68.  
  69. /** 
  70. * Gets all Headers 
  71. * @return array 
  72. */ 
  73. public function getHeaders() 
  74. return $this->headers; 
  75.  
  76. /** 
  77. * Get Header by Name 
  78. * @param $name 
  79. * @return string|null 
  80. */ 
  81. public function getHeader($name) 
  82. if (array_key_exists($name, $this->headers)) { 
  83. return $this->headers[$name]; 
  84. return null; 
  85.  
  86. /** 
  87. * Sets Url 
  88. * @param $url 
  89. */ 
  90. public function setUrl($url) 
  91. $this->url = $url; 
  92.  
  93. /** 
  94. * Set Headers 
  95. * @param array $headers 
  96. */ 
  97. public function setHeaders(array $headers = array()) 
  98. $this->headers = $headers; 
  99.  
  100. /** 
  101. * Adds a Header 
  102. * @param $name 
  103. * @param $value 
  104. * @param bool $overWrite allows you to override header value 
  105. */ 
  106. public function addHeader($name, $value, $overWrite = true) 
  107. if (!array_key_exists($name, $this->headers) || $overWrite) { 
  108. $this->headers[$name] = $value; 
  109. } else { 
  110. $this->headers[$name] = $this->headers[$name] . self::HEADER_SEPARATOR . $value; 
  111.  
  112. /** 
  113. * Removes a Header 
  114. * @param $name 
  115. */ 
  116. public function removeHeader($name) 
  117. unset($this->headers[$name]); 
  118.  
  119. /** 
  120. * Gets all curl options 
  121. * @return array 
  122. */ 
  123. public function getCurlOptions() 
  124. return $this->curlOptions; 
  125.  
  126. /** 
  127. * Add Curl Option 
  128. * @param string $name 
  129. * @param mixed $value 
  130. */ 
  131. public function addCurlOption($name, $value) 
  132. $this->curlOptions[$name] = $value; 
  133.  
  134. /** 
  135. * Removes a curl option from the list 
  136. * @param $name 
  137. */ 
  138. public function removeCurlOption($name) 
  139. unset($this->curlOptions[$name]); 
  140.  
  141. /** 
  142. * Set Curl Options. Overrides all curl options 
  143. * @param $options 
  144. */ 
  145. public function setCurlOptions($options) 
  146. $this->curlOptions = $options; 
  147.  
  148. /** 
  149. * Set ssl parameters for certificate based client authentication 
  150. * @param $certPath 
  151. * @param null $passPhrase 
  152. */ 
  153. public function setSSLCert($certPath, $passPhrase = null) 
  154. $this->curlOptions[CURLOPT_SSLCERT] = realpath($certPath); 
  155. if (isset($passPhrase) && trim($passPhrase) != "") { 
  156. $this->curlOptions[CURLOPT_SSLCERTPASSWD] = $passPhrase; 
  157.  
  158. /** 
  159. * Set connection timeout in seconds 
  160. * @param integer $timeout 
  161. */ 
  162. public function setHttpTimeout($timeout) 
  163. $this->curlOptions[CURLOPT_CONNECTTIMEOUT] = $timeout; 
  164.  
  165. /** 
  166. * Set HTTP proxy information 
  167. * @param string $proxy 
  168. * @throws PayPalConfigurationException 
  169. */ 
  170. public function setHttpProxy($proxy) 
  171. $urlParts = parse_url($proxy); 
  172. if ($urlParts == false || !array_key_exists("host", $urlParts)) { 
  173. throw new PayPalConfigurationException("Invalid proxy configuration " . $proxy); 
  174. $this->curlOptions[CURLOPT_PROXY] = $urlParts["host"]; 
  175. if (isset($urlParts["port"])) { 
  176. $this->curlOptions[CURLOPT_PROXY] .= ":" . $urlParts["port"]; 
  177. if (isset($urlParts["user"])) { 
  178. $this->curlOptions[CURLOPT_PROXYUSERPWD] = $urlParts["user"] . ":" . $urlParts["pass"]; 
  179.  
  180. /** 
  181. * Set Http Retry Counts 
  182. * @param int $retryCount 
  183. */ 
  184. public function setHttpRetryCount($retryCount) 
  185. $this->retryCount = $retryCount; 
  186.  
  187. /** 
  188. * Get Http Retry Counts 
  189. * @return int 
  190. */ 
  191. public function getHttpRetryCount() 
  192. return $this->retryCount; 
  193.  
  194. /** 
  195. * Sets the User-Agent string on the HTTP request 
  196. * @param string $userAgentString 
  197. */ 
  198. public function setUserAgent($userAgentString) 
  199. $this->curlOptions[CURLOPT_USERAGENT] = $userAgentString; 
  200.  
  201. /** 
  202. * Retrieves an array of constant key, and value based on Prefix 
  203. * @param array $configs 
  204. * @param $prefix 
  205. * @return array 
  206. */ 
  207. public function getHttpConstantsFromConfigs($configs = array(), $prefix) 
  208. $arr = array(); 
  209. if ($prefix != null && is_array($configs)) { 
  210. foreach ($configs as $k => $v) { 
  211. // Check if it startsWith 
  212. if (substr($k, 0, strlen($prefix)) === $prefix) { 
  213. $newKey = ltrim($k, $prefix); 
  214. if (defined($newKey)) { 
  215. $arr[constant($newKey)] = $v; 
  216. return $arr;