1 <?php
2
3 namespace PayPal\Core;
4
5 use PayPal\Exception\PayPalConfigurationException;
6 use PayPal\Exception\PayPalConnectionException;
7
8 9 10 11 12
13 class PayPalHttpConnection
14 {
15
16 17 18
19 private $httpConfig;
20
21 22 23 24 25
26 private $logger;
27
28 29 30
31 private $responseHeaders = array();
32
33 34 35
36 private $skippedHttpStatusLine = false;
37
38 39 40 41 42 43 44
45 public function __construct(PayPalHttpConfig $httpConfig, array $config)
46 {
47 if (!function_exists("curl_init")) {
48 throw new PayPalConfigurationException("Curl module is not available on this system");
49 }
50 $this->httpConfig = $httpConfig;
51 $this->logger = PayPalLoggingManager::getInstance(__CLASS__);
52 }
53
54 55 56 57 58
59 private function getHttpHeaders()
60 {
61 $ret = array();
62 foreach ($this->httpConfig->getHeaders() as $k => $v) {
63 $ret[] = "$k: $v";
64 }
65 return $ret;
66 }
67
68 69 70 71 72 73 74
75 protected function parseResponseHeaders($ch, $data) {
76 if (!$this->skippedHttpStatusLine) {
77 $this->skippedHttpStatusLine = true;
78 return strlen($data);
79 }
80
81 $trimmedData = trim($data);
82 if (strlen($trimmedData) == 0) {
83 return strlen($data);
84 }
85
86 list($key, $value) = explode(":", $trimmedData, 2);
87
88 $key = trim($key);
89 $value = trim($value);
90
91
92
93 if (strlen($key) > 0 && strlen($value) > 0) {
94
95
96
97 $this->responseHeaders[$key] = $value;
98 }
99
100 return strlen($data);
101 }
102
103
104 105 106 107 108 109
110 protected function implodeArray($arr) {
111 $retStr = '';
112 foreach($arr as $key => $value) {
113 $retStr .= $key . ': ' . $value . ', ';
114 }
115 rtrim($retStr, ', ');
116 return $retStr;
117 }
118
119 120 121 122 123 124 125
126 public function execute($data)
127 {
128
129 $this->logger->info($this->httpConfig->getMethod() . ' ' . $this->httpConfig->getUrl());
130
131
132 $ch = curl_init($this->httpConfig->getUrl());
133 $options = $this->httpConfig->getCurlOptions();
134 if (empty($options[CURLOPT_HTTPHEADER])) {
135 unset($options[CURLOPT_HTTPHEADER]);
136 }
137 curl_setopt_array($ch, $options);
138 curl_setopt($ch, CURLOPT_URL, $this->httpConfig->getUrl());
139 curl_setopt($ch, CURLOPT_HEADER, false);
140 curl_setopt($ch, CURLINFO_HEADER_OUT, true);
141 curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHttpHeaders());
142
143
144 switch ($this->httpConfig->getMethod()) {
145 case 'POST':
146 curl_setopt($ch, CURLOPT_POST, true);
147 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
148 break;
149 case 'PUT':
150 case 'PATCH':
151 case 'DELETE':
152 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
153 break;
154 }
155
156
157 if ($this->httpConfig->getMethod() != null) {
158 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->httpConfig->getMethod());
159 }
160
161 $this->responseHeaders = array();
162 $this->skippedHttpStatusLine = false;
163 curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'parseResponseHeaders'));
164
165
166 $result = curl_exec($ch);
167
168 $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
169
170
171 if (curl_errno($ch) == 60) {
172 $this->logger->info("Invalid or no certificate authority found - Retrying using bundled CA certs file");
173 curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
174 $result = curl_exec($ch);
175
176 $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
177 }
178
179
180 if (curl_errno($ch)) {
181 $ex = new PayPalConnectionException(
182 $this->httpConfig->getUrl(),
183 curl_error($ch),
184 curl_errno($ch)
185 );
186 curl_close($ch);
187 throw $ex;
188 }
189
190
191 $requestHeaders = curl_getinfo($ch, CURLINFO_HEADER_OUT);
192 $this->logger->debug("Request Headers \t: " . str_replace("\r\n", ", ", $requestHeaders));
193 $this->logger->debug(($data && $data != '' ? "Request Data\t\t: " . $data : "No Request Payload") . "\n" . str_repeat('-', 128) . "\n");
194 $this->logger->info("Response Status \t: " . $httpStatus);
195 $this->logger->debug("Response Headers\t: " . $this->implodeArray($this->responseHeaders));
196
197
198 curl_close($ch);
199
200
201 if ($httpStatus < 200 || $httpStatus >= 300) {
202 $ex = new PayPalConnectionException(
203 $this->httpConfig->getUrl(),
204 "Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}.",
205 $httpStatus
206 );
207 $ex->setData($result);
208 $this->logger->error("Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}. " . $result);
209 $this->logger->debug("\n\n" . str_repeat('=', 128) . "\n");
210 throw $ex;
211 }
212
213 $this->logger->debug(($result && $result != '' ? "Response Data \t: " . $result : "No Response Body") . "\n\n" . str_repeat('=', 128) . "\n");
214
215
216 return $result;
217 }
218 }
219