PluginProbe
WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress / 8.5.37
WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress v8.5.37
9.1.3 9.1.2 9.1.1 9.1.0 9.0.3 9.0.2 9.0.1 9.0.0 8.5.79 8.5.78 8.5.77 8.5.76 8.5.75 8.5.74 8.5.73 8.5.72 8.5.71 8.5.70 8.5.69 8.5.68 8.5.35 8.5.36 8.5.37 8.5.38 8.5.39 All 222 releases
wpvr / vendor / posthog / posthog-php / lib / HttpClient.php

HttpClient.php in WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress 8.5.37, at vendor/posthog/posthog-php/lib/HttpClient.php

159 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace PostHog;
4
5 use Closure;
6
7 class HttpClient
8 {
9 /**
10 * @var string
11 */
12 private $host;
13
14 /**
15 * @var bool
16 */
17 private $useSsl;
18
19 /**
20 * @var int
21 */
22 private $maximumBackoffDuration;
23
24 /**
25 * @var bool
26 */
27 private $compressRequests;
28
29 /**
30 * @var Closure|null
31 */
32 private $errorHandler;
33 /**
34 * @var bool
35 */
36 private $debug;
37
38 /**
39 * @var int The maximum number of milliseconds to allow cURL functions to execute / wait.
40 */
41 private $curlTimeoutMilliseconds;
42
43 public function __construct(
44 string $host,
45 bool $useSsl = true,
46 int $maximumBackoffDuration = 10000,
47 bool $compressRequests = false,
48 bool $debug = false,
49 ?Closure $errorHandler = null,
50 int $curlTimeoutMilliseconds = 10000
51 ) {
52 $this->host = $host;
53 $this->useSsl = $useSsl;
54 $this->maximumBackoffDuration = $maximumBackoffDuration;
55 $this->compressRequests = $compressRequests;
56 $this->debug = $debug;
57 $this->errorHandler = $errorHandler;
58 $this->curlTimeoutMilliseconds = $curlTimeoutMilliseconds;
59 }
60
61 /**
62 * @param string $path
63 * @param string|null $payload
64 * @param array $extraHeaders
65 * @param array $requestOptions
66 * @return HttpResponse
67 */
68 public function sendRequest(string $path, ?string $payload, array $extraHeaders = [], array $requestOptions = []): HttpResponse
69 {
70 $protocol = $this->useSsl ? "https://" : "http://";
71
72 $backoff = 100; // Set initial waiting time to 100ms
73
74 $shouldRetry = $requestOptions['shouldRetry'] ?? true;
75 $shouldVerify = $requestOptions['shouldVerify'] ?? true;
76
77 do {
78 // open connection
79 $ch = curl_init();
80
81 if (null !== $payload) {
82 curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
83 }
84
85 $headers = [];
86 $headers[] = 'Content-Type: application/json';
87 if ($this->compressRequests) {
88 $headers[] = 'Content-Encoding: gzip';
89 }
90
91 // check if timeout exists in request options, if not use default
92 $timeout = $this->curlTimeoutMilliseconds;
93 if (isset($requestOptions['timeout'])) {
94 $timeout = $requestOptions['timeout'];
95 }
96
97 curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($headers, $extraHeaders));
98 curl_setopt($ch, CURLOPT_URL, $protocol . $this->host . $path);
99 curl_setopt($ch, CURLOPT_RETURNTRANSFER, $shouldVerify);
100 curl_setopt($ch, CURLOPT_TIMEOUT_MS, $shouldVerify ? $timeout : 1);
101 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $timeout);
102 if (! $shouldVerify) {
103 curl_setopt($ch, CURLOPT_NOSIGNAL, true);
104 curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
105 }
106
107 // retry failed requests just once to diminish impact on performance
108 $httpResponse = $this->executePost($ch);
109 $responseCode = $httpResponse->getResponseCode();
110
111 //close connection
112 curl_close($ch);
113
114 if ($shouldVerify && 200 != $responseCode) {
115 // log error
116 $this->handleError($ch, $responseCode);
117
118 if ($shouldRetry === false) {
119 break;
120 } elseif (($responseCode >= 500 && $responseCode <= 600) || 429 == $responseCode) {
121 // If status code is greater than 500 and less than 600, it indicates server error
122 // Error code 429 indicates rate limited.
123 // Retry uploading in these cases.
124 usleep($backoff * 1000);
125 $backoff *= 2;
126 } elseif ($responseCode >= 400) {
127 break;
128 } elseif ($responseCode == 0) {
129 break;
130 }
131 } else {
132 break; // no error
133 }
134 } while ($shouldRetry && $backoff < $this->maximumBackoffDuration);
135
136 return $httpResponse;
137 }
138
139 private function executePost($ch): HttpResponse
140 {
141 return new HttpResponse(
142 curl_exec($ch),
143 curl_getinfo($ch, CURLINFO_RESPONSE_CODE)
144 );
145 }
146
147 private function handleError($code, $message)
148 {
149 if (null !== $this->errorHandler) {
150 $handler = $this->errorHandler;
151 $handler($code, $message);
152 }
153
154 if ($this->debug) {
155 error_log("[PostHog][HttpClient] " . $message);
156 }
157 }
158 }
159