| 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 |
//close connection |
| 98 |
curl_close($ch); |
| 99 |
|
| 100 |
if (200 != $responseCode) { |
| 101 |
// log error |
| 102 |
$this->handleError($ch, $responseCode); |
| 103 |
|
| 104 |
if (($responseCode >= 500 && $responseCode <= 600) || 429 == $responseCode) { |
| 105 |
// If status code is greater than 500 and less than 600, it indicates server error |
| 106 |
// Error code 429 indicates rate limited. |
| 107 |
// Retry uploading in these cases. |
| 108 |
usleep($backoff * 1000); |
| 109 |
$backoff *= 2; |
| 110 |
} elseif ($responseCode >= 400) { |
| 111 |
break; |
| 112 |
} elseif ($responseCode == 0) { |
| 113 |
break; |
| 114 |
} |
| 115 |
} else { |
| 116 |
break; // no error |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return $httpResponse; |
| 121 |
} |
| 122 |
|
| 123 |
private function executePost($ch): HttpResponse |
| 124 |
{ |
| 125 |
return new HttpResponse( |
| 126 |
curl_exec($ch), |
| 127 |
curl_getinfo($ch, CURLINFO_RESPONSE_CODE) |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
private function handleError($code, $message) |
| 132 |
{ |
| 133 |
if (null !== $this->errorHandler) { |
| 134 |
$handler = $this->errorHandler; |
| 135 |
$handler($code, $message); |
| 136 |
} |
| 137 |
|
| 138 |
if ($this->debug) { |
| 139 |
error_log("[PostHog][HttpClient] " . $message); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|