PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.11.0
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.11.0
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / vendor / posthog / posthog-php / lib / HttpClient.php

HttpClient.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.11.0, at vendor/posthog/posthog-php/lib/HttpClient.php

140 lines 3.8 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 = 750
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 * @return HttpResponse
66 */
67 public function sendRequest(string $path, ?string $payload, array $extraHeaders = []): HttpResponse
68 {
69 $protocol = $this->useSsl ? "https://" : "http://";
70
71 $backoff = 100; // Set initial waiting time to 100ms
72
73 while ($backoff < $this->maximumBackoffDuration) {
74 // open connection
75 $ch = curl_init();
76
77 if (null !== $payload) {
78 curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
79 }
80
81 $headers = [];
82 $headers[] = 'Content-Type: application/json';
83 if ($this->compressRequests) {
84 $headers[] = 'Content-Encoding: gzip';
85 }
86
87 curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($headers, $extraHeaders));
88 curl_setopt($ch, CURLOPT_URL, $protocol . $this->host . $path);
89 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
90 curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->curlTimeoutMilliseconds);
91 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->curlTimeoutMilliseconds);
92
93 // retry failed requests just once to diminish impact on performance
94 $httpResponse = $this->executePost($ch);
95 $responseCode = $httpResponse->getResponseCode();
96
97 if (200 != $responseCode) {
98 // log error
99 $this->handleError($ch, $responseCode);
100
101 if (($responseCode >= 500 && $responseCode <= 600) || 429 == $responseCode) {
102 // If status code is greater than 500 and less than 600, it indicates server error
103 // Error code 429 indicates rate limited.
104 // Retry uploading in these cases.
105 usleep($backoff * 1000);
106 $backoff *= 2;
107 } elseif ($responseCode >= 400) {
108 break;
109 } elseif ($responseCode == 0) {
110 break;
111 }
112 } else {
113 break; // no error
114 }
115 }
116
117 return $httpResponse;
118 }
119
120 private function executePost($ch): HttpResponse
121 {
122 return new HttpResponse(
123 curl_exec($ch),
124 curl_getinfo($ch, CURLINFO_RESPONSE_CODE)
125 );
126 }
127
128 private function handleError($code, $message)
129 {
130 if (null !== $this->errorHandler) {
131 $handler = $this->errorHandler;
132 $handler($code, $message);
133 }
134
135 if ($this->debug) {
136 error_log("[PostHog][HttpClient] " . $message);
137 }
138 }
139 }
140