3 use PayPal\Exception\PPConfigurationException;
7 * Some default options for curl
8 * These are typically overridden by PPConnectionManager
10 public static $DEFAULT_CURL_OPTS = array(
11 CURLOPT_SSLVERSION => 3,
12 CURLOPT_CONNECTTIMEOUT => 10,
13 CURLOPT_RETURNTRANSFER => TRUE,
14 CURLOPT_TIMEOUT => 60, // maximum number of seconds to allow cURL functions to execute
15 CURLOPT_USERAGENT => 'PayPal-PHP-SDK',
16 CURLOPT_HTTPHEADER => array(),
17 CURLOPT_SSL_VERIFYHOST => 2,
18 CURLOPT_SSL_VERIFYPEER => 1
21 const HEADER_SEPARATOR = ';';
22 const HTTP_GET = 'GET';
23 const HTTP_POST = 'POST';
25 private $headers = array();
33 * Number of times to retry a failed HTTP call
40 * @param string $method HTTP method (GET, POST etc) defaults to POST
42 public function __construct($url=null, $method=self::HTTP_POST) {
44 $this->method = $method;
45 $this->curlOptions = self::$DEFAULT_CURL_OPTS;
48 public function getUrl() {
52 public function getMethod() {
56 public function getHeaders() {
57 return $this->headers;
60 public function getHeader($name) {
61 if(array_key_exists($name, $this->headers)) {
62 return $this->headers[$name];
67 public function setUrl($url) {
71 public function setHeaders(array $headers) {
72 $this->headers = $headers;
75 public function addHeader($name, $value, $overWrite=true) {
76 if(!array_key_exists($name, $this->headers) || $overWrite) {
77 $this->headers[$name] = $value;
79 $this->headers[$name] = $this->headers[$name] . HEADER_SEPARATOR . $value;
83 public function removeHeader($name) {
84 unset($this->headers[$name]);
89 public function getCurlOptions() {
90 return $this->curlOptions;
93 public function addCurlOption($name, $value) {
94 $this->curlOptions[$name] = $value;
97 public function setCurlOptions($options) {
98 $this->curlOptions = $options;
104 * Set ssl parameters for certificate based client authentication
106 * @param string $certPath - path to client certificate file (PEM formatted file)
108 public function setSSLCert($certPath, $passPhrase=NULL) {
109 $this->curlOptions[CURLOPT_SSLCERT] = realpath($certPath);
110 if(isset($passPhrase) && trim($passPhrase) != "") {
111 $this->curlOptions[CURLOPT_SSLCERTPASSWD] = $passPhrase;
116 * Set connection timeout in seconds
117 * @param integer $timeout
119 public function setHttpTimeout($timeout) {
120 $this->curlOptions[CURLOPT_CONNECTTIMEOUT] = $timeout;
124 * Set HTTP proxy information
125 * @param string $proxy
126 * @throws PPConfigurationException
128 public function setHttpProxy($proxy) {
129 $urlParts = parse_url($proxy);
130 if($urlParts == false || !array_key_exists("host", $urlParts))
131 throw new PPConfigurationException("Invalid proxy configuration ".$proxy);
133 $this->curlOptions[CURLOPT_PROXY] = $urlParts["host"];
134 if(isset($urlParts["port"]))
135 $this->curlOptions[CURLOPT_PROXY] .= ":" . $urlParts["port"];
136 if(isset($urlParts["user"]))
137 $this->curlOptions[URLOPT_PROXYUSERPWD] = $urlParts["user"] . ":" . $urlParts["pass"];
141 * @param integer $retry
143 public function setHttpRetryCount($retryCount) {
144 $this->retryCount = $retryCount;
147 public function getHttpRetryCount() {
148 return $this->retryCount;
152 * Sets the User-Agent string on the HTTP request
153 * @param string $userAgentString
155 public function setUserAgent($userAgentString) {
156 $this->curlOptions[CURLOPT_USERAGENT] = $userAgentString;