PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.22.3
GiveWP – Donation Plugin and Fundraising Platform v2.22.3
4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / vendor / paypal / paypalhttp / lib / PayPalHttp / HttpClient.php
give / vendor / paypal / paypalhttp / lib / PayPalHttp Last commit date
Serializer 5 years ago Curl.php 5 years ago Encoder.php 4 years ago Environment.php 5 years ago HttpClient.php 4 years ago HttpException.php 4 years ago HttpRequest.php 5 years ago HttpResponse.php 4 years ago IOException.php 5 years ago Injector.php 4 years ago Serializer.php 5 years ago
HttpClient.php
239 lines
1 <?php
2
3 namespace PayPalHttp;
4
5 /**
6 * Class HttpClient
7 * @package PayPalHttp
8 *
9 * Client used to make HTTP requests.
10 */
11 class HttpClient
12 {
13 /**
14 * @var Environment
15 */
16 public $environment;
17
18 /**
19 * @var Injector[]
20 */
21 public $injectors = [];
22
23 /**
24 * @var Encoder
25 */
26 public $encoder;
27
28 /**
29 * HttpClient constructor. Pass the environment you wish to make calls to.
30 *
31 * @param Environment $environment
32 * @see Environment
33 */
34 function __construct(Environment $environment)
35 {
36 $this->environment = $environment;
37 $this->encoder = new Encoder();
38 $this->curlCls = Curl::class;
39 }
40
41 /**
42 * Injectors are blocks that can be used for executing arbitrary pre-flight logic, such as modifying a request or logging data.
43 * Executed in first-in first-out order.
44 *
45 * @param Injector $inj
46 */
47 public function addInjector(Injector $inj)
48 {
49 $this->injectors[] = $inj;
50 }
51
52 /**
53 * The method that takes an HTTP request, serializes the request, makes a call to given environment, and deserialize response
54 *
55 * @param HttpRequest $httpRequest
56 * @return HttpResponse
57 *
58 * @throws HttpException
59 * @throws IOException
60 */
61 public function execute(HttpRequest $httpRequest)
62 {
63 $requestCpy = clone $httpRequest;
64 $curl = new Curl();
65
66 foreach ($this->injectors as $inj) {
67 $inj->inject($requestCpy);
68 }
69
70 $url = $this->environment->baseUrl() . $requestCpy->path;
71 $formattedHeaders = $this->prepareHeaders($requestCpy->headers);
72 if (!array_key_exists("user-agent", $formattedHeaders)) {
73 $requestCpy->headers["user-agent"] = $this->userAgent();
74 }
75
76 $body = "";
77 if (!is_null($requestCpy->body)) {
78 $rawHeaders = $requestCpy->headers;
79 $requestCpy->headers = $formattedHeaders;
80 $body = $this->encoder->serializeRequest($requestCpy);
81 $requestCpy->headers = $this->mapHeaders($rawHeaders,$requestCpy->headers);
82 }
83
84 $curl->setOpt(CURLOPT_URL, $url);
85 $curl->setOpt(CURLOPT_CUSTOMREQUEST, $requestCpy->verb);
86 $curl->setOpt(CURLOPT_HTTPHEADER, $this->serializeHeaders($requestCpy->headers));
87 $curl->setOpt(CURLOPT_RETURNTRANSFER, 1);
88 $curl->setOpt(CURLOPT_HEADER, 0);
89
90 if (!is_null($requestCpy->body)) {
91 $curl->setOpt(CURLOPT_POSTFIELDS, $body);
92 }
93
94 if (strpos($this->environment->baseUrl(), "https://") === 0) {
95 $curl->setOpt(CURLOPT_SSL_VERIFYPEER, true);
96 $curl->setOpt(CURLOPT_SSL_VERIFYHOST, 2);
97 }
98
99 if ($caCertPath = $this->getCACertFilePath()) {
100 $curl->setOpt(CURLOPT_CAINFO, $caCertPath);
101 }
102
103 $response = $this->parseResponse($curl);
104 $curl->close();
105
106 return $response;
107 }
108
109 /**
110 * Returns an array representing headers with their keys
111 * to be lower case
112 * @param $headers
113 * @return array
114 */
115 public function prepareHeaders($headers){
116 $preparedHeaders = array_change_key_case($headers);
117 if (array_key_exists("content-type", $preparedHeaders)) {
118 $preparedHeaders["content-type"] = strtolower($preparedHeaders["content-type"]);
119 }
120 return $preparedHeaders;
121 }
122
123 /**
124 * Returns an array representing headers with their key in
125 * original cases and updated values
126 * @param $rawHeaders
127 * @param $formattedHeaders
128 * @return array
129 */
130 public function mapHeaders($rawHeaders, $formattedHeaders){
131 $rawHeadersKey = array_keys($rawHeaders);
132 foreach ($rawHeadersKey as $array_key) {
133 if(array_key_exists(strtolower($array_key), $formattedHeaders)){
134 $rawHeaders[$array_key] = $formattedHeaders[strtolower($array_key)];
135 }
136 }
137 return $rawHeaders;
138 }
139
140 /**
141 * Returns default user-agent
142 *
143 * @return string
144 */
145 public function userAgent()
146 {
147 return "PayPalHttp-PHP HTTP/1.1";
148 }
149
150 /**
151 * Return the filepath to your custom CA Cert if needed.
152 * @return string
153 */
154 protected function getCACertFilePath()
155 {
156 return null;
157 }
158
159 protected function setCurl(Curl $curl)
160 {
161 $this->curl = $curl;
162 }
163
164 protected function setEncoder(Encoder $encoder)
165 {
166 $this->encoder = $encoder;
167 }
168
169 private function serializeHeaders($headers)
170 {
171 $headerArray = [];
172 if ($headers) {
173 foreach ($headers as $key => $val) {
174 $headerArray[] = $key . ": " . $val;
175 }
176 }
177
178 return $headerArray;
179 }
180
181 private function parseResponse($curl)
182 {
183 $headers = [];
184 $curl->setOpt(CURLOPT_HEADERFUNCTION,
185 function($curl, $header) use (&$headers)
186 {
187 $len = strlen($header);
188
189 $k = "";
190 $v = "";
191
192 $this->deserializeHeader($header, $k, $v);
193 $headers[$k] = $v;
194
195 return $len;
196 });
197
198 $responseData = $curl->exec();
199 $statusCode = $curl->getInfo(CURLINFO_HTTP_CODE);
200 $errorCode = $curl->errNo();
201 $error = $curl->error();
202
203 if ($errorCode > 0) {
204 throw new IOException($error, $errorCode);
205 }
206
207 $body = $responseData;
208
209 if ($statusCode >= 200 && $statusCode < 300) {
210 $responseBody = NULL;
211
212 if (!empty($body)) {
213 $responseBody = $this->encoder->deserializeResponse($body, $this->prepareHeaders($headers));
214 }
215
216 return new HttpResponse(
217 $errorCode === 0 ? $statusCode : $errorCode,
218 $responseBody,
219 $headers
220 );
221 } else {
222 throw new HttpException($body, $statusCode, $headers);
223 }
224 }
225
226 private function deserializeHeader($header, &$key, &$value)
227 {
228 if (strlen($header) > 0) {
229 if (empty($header) || strpos($header, ':') === false) {
230 return NULL;
231 }
232
233 list($k, $v) = explode(":", $header);
234 $key = trim($k);
235 $value = trim($v);
236 }
237 }
238 }
239