PPHttpConfig.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. class PPHttpConfig {
  3. /**
  4. * Some default options for curl
  5. * These are typically overridden by PPConnectionManager
  6. */
  7. public static $DEFAULT_CURL_OPTS = array(
  8. CURLOPT_CONNECTTIMEOUT => 10,
  9. CURLOPT_RETURNTRANSFER => TRUE,
  10. CURLOPT_TIMEOUT => 60, // maximum number of seconds to allow cURL functions to execute
  11. CURLOPT_USERAGENT => 'PayPal-PHP-SDK',
  12. CURLOPT_POST => 1,
  13. CURLOPT_HTTPHEADER => array(),
  14. CURLOPT_SSL_VERIFYHOST => 2,
  15. CURLOPT_SSL_VERIFYPEER => 1
  16. );
  17. const HEADER_SEPARATOR = ';';
  18. const HTTP_GET = 'GET';
  19. const HTTP_POST = 'POST';
  20. private $headers = array();
  21. private $curlOptions;
  22. private $url;
  23. private $method;
  24. /***
  25. * Number of times to retry a failed HTTP call
  26. */
  27. private $retryCount;
  28. /**
  29. *
  30. * @param string $url
  31. * @param string $method HTTP method (GET, POST etc) defaults to POST
  32. */
  33. public function __construct($url, $method=self::HTTP_POST) {
  34. $this->url = $url;
  35. $this->method = $method;
  36. $this->curlOptions = self::$DEFAULT_CURL_OPTS;
  37. }
  38. public function getUrl() {
  39. return $this->url;
  40. }
  41. public function getMethod() {
  42. return $this->method;
  43. }
  44. public function getHeaders() {
  45. return $this->headers;
  46. }
  47. public function getHeader($name) {
  48. if(array_key_exists($name, $this->headers)) {
  49. return $this->headers[$name];
  50. }
  51. return NULL;
  52. }
  53. public function setHeaders(array $headers) {
  54. $this->headers = $headers;
  55. }
  56. public function addHeader($name, $value, $overWrite=true) {
  57. if(!array_key_exists($name, $this->headers) || $overWrite) {
  58. $this->headers[$name] = $value;
  59. } else {
  60. $this->headers[$name] = $this->headers[$name] . HEADER_SEPARATOR . $value;
  61. }
  62. }
  63. public function removeHeader($name) {
  64. unset($this->headers[$name]);
  65. }
  66. public function getCurlOptions() {
  67. return $this->curlOptions;
  68. }
  69. public function addCurlOption($name, $value) {
  70. $this->curlOptions[$name] = $value;
  71. }
  72. public function setCurlOptions($options) {
  73. $this->curlOptions = $options;
  74. }
  75. /**
  76. * Set ssl parameters for certificate based client authentication
  77. *
  78. * @param string $certPath - path to client certificate file (PEM formatted file)
  79. */
  80. public function setSSLCert($certPath, $passPhrase=NULL) {
  81. $this->curlOptions[CURLOPT_SSLCERT] = realpath($certPath);
  82. if(isset($passPhrase) && trim($passPhrase) != "") {
  83. $this->curlOptions[CURLOPT_SSLCERTPASSWD] = $passPhrase;
  84. }
  85. }
  86. /**
  87. * Set connection timeout in seconds
  88. * @param integer $timeout
  89. */
  90. public function setHttpTimeout($timeout) {
  91. $this->curlOptions[CURLOPT_CONNECTTIMEOUT] = $timeout;
  92. }
  93. /**
  94. * Set HTTP proxy information
  95. * @param string $proxy
  96. * @throws PPConfigurationException
  97. */
  98. public function setHttpProxy($proxy) {
  99. $urlParts = parse_url($proxy);
  100. if($urlParts == false || !array_key_exists("host", $urlParts))
  101. throw new PPConfigurationException("Invalid proxy configuration ".$proxy);
  102. $this->curlOptions[CURLOPT_PROXY] = $urlParts["host"];
  103. if(isset($urlParts["port"]))
  104. $this->curlOptions[CURLOPT_PROXY] .= ":" . $urlParts["port"];
  105. if(isset($urlParts["user"]))
  106. $this->curlOptions[URLOPT_PROXYUSERPWD] = $urlParts["user"] . ":" . $urlParts["pass"];
  107. }
  108. /**
  109. * @param integer $retry
  110. */
  111. public function setHttpRetryCount($retryCount) {
  112. $this->retryCount = $retryCount;
  113. }
  114. public function getHttpRetryCount() {
  115. return $this->retryCount;
  116. }
  117. }