3 use PayPal\Core\PPLoggingManager;
4 use PayPal\Exception\PPConfigurationException;
5 use PayPal\Exception\PPConnectionException;
8 * A wrapper class based on the curl extension.
9 * Requires the PHP curl module to be enabled.
10 * See for full requirements the PHP manual: http://php.net/curl
14 class PPHttpConnection
20 * HTTP status codes for which a retry must be attempted
22 private static $retryCodes = array('401', '403', '404', );
26 public function __construct($httpConfig, $config)
28 if( !function_exists("curl_init") ) {
29 throw new PPConfigurationException("Curl module is not available on this system");
31 $this->httpConfig = $httpConfig;
32 $this->logger = new PPLoggingManager(__CLASS__, $config);
35 private function getHttpHeaders() {
38 foreach($this->httpConfig->getHeaders() as $k=>$v) {
45 * Executes an HTTP request
47 * @param string $data query string OR POST content as a string
48 * @throws PPConnectionException
50 public function execute($data) {
51 $this->logger->fine("Connecting to " . $this->httpConfig->getUrl());
52 $this->logger->fine("Payload " . $data);
54 $ch = curl_init($this->httpConfig->getUrl());
55 curl_setopt_array($ch, $this->httpConfig->getCurlOptions());
56 curl_setopt($ch, CURLOPT_URL, $this->httpConfig->getUrl());
57 curl_setopt($ch, CURLOPT_HEADER, false);
58 curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHttpHeaders());
60 switch($this->httpConfig->getMethod()) {
62 curl_setopt($ch, CURLOPT_POST, true);
63 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
66 if($this->httpConfig->getMethod() != NULL) {
67 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->httpConfig->getMethod());
69 foreach($this->getHttpHeaders() as $header) {
70 //TODO: Strip out credentials and other secure info when logging.
71 $this->logger->info("Adding header $header");
73 $result = curl_exec($ch);
74 if ((curl_errno($ch) == 60) && ($this->httpConfig->getMethod() != "DELETE")) {
75 $this->logger->info("Invalid or no certificate authority found - Retrying using bundled CA certs file");
76 curl_setopt($ch, CURLOPT_CAINFO,
77 dirname(__FILE__) . '/cacert.pem');
78 $result = curl_exec($ch);
80 $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
82 if(in_array($httpStatus, self::$retryCodes) && $this->httpConfig->getHttpRetryCount() != null) {
83 $this->logger->info("Got $httpStatus response from server. Retrying");
86 $result = curl_exec($ch);
87 $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
88 } while (in_array($httpStatus, self::$retryCodes) && (++$retries < $this->httpConfig->getHttpRetryCount()) );
90 if ( curl_errno($ch) ) {
91 $ex = new PPConnectionException($this->httpConfig->getUrl(), curl_error($ch), curl_errno($ch));
98 if(in_array($httpStatus, self::$retryCodes)) {
99 $ex = new PPConnectionException($this->httpConfig->getUrl() ,
100 "Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}. Retried $retries times.");
101 $ex->setData($result);
103 } else if($httpStatus < 200 || $httpStatus >=300) {
104 $ex = new PPConnectionException($this->httpConfig->getUrl() ,
105 "Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}.");
106 $ex->setData($result);