-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathLibCurl.php
More file actions
84 lines (74 loc) · 2.42 KB
/
LibCurl.php
File metadata and controls
84 lines (74 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace PostHog\Consumer;
use PostHog\HttpClient;
use PostHog\QueueConsumer;
class LibCurl extends QueueConsumer
{
protected $type = "LibCurl";
/**
* @var HttpClient
*/
private $httpClient;
/**
* Creates a new queued libcurl consumer
* @param string $apiKey
* @param array $options
* boolean "debug" - whether to use debug output, wait for response.
* number "max_queue_size" - the max size of messages to enqueue
* number "batch_size" - how many messages to send in a single request
*/
public function __construct($apiKey, $options = [], ?HttpClient $httpClient = null)
{
parent::__construct($apiKey, $options);
$this->httpClient = $httpClient !== null ? $httpClient : new HttpClient(
$this->host,
$this->ssl(),
$this->maximum_backoff_duration,
$this->compress_request,
$this->debug(),
$this->options['error_handler'] ?? null
);
}
/**
* Define getter method for consumer type
*
* @return string
*/
public function getConsumer()
{
return $this->type;
}
/**
* Make a sync request to our API. If debug is
* enabled, we wait for the response
* and retry once to diminish impact on performance.
* @param array $messages array of all the messages to send
* @return boolean whether the request succeeded
*/
public function flushBatch($messages)
{
$body = $this->payload($messages);
$payload = json_encode($body);
if (strlen($payload) >= self::MAX_BATCH_PAYLOAD_SIZE) {
if ($this->debug()) {
$msg = "Message size is larger than " . self::MAX_BATCH_PAYLOAD_SIZE_HUMAN;
error_log("[PostHog][" . $this->type . "] " . $msg);
}
return false;
}
if ($this->compress_request) {
$payload = gzencode($payload);
}
return $this->httpClient->sendRequest(
'/batch/',
$payload,
[
// Send user agent in the form of {library_name}/{library_version} as per RFC 7231.
"User-Agent: {$messages[0]['library']}/{$messages[0]['library_version']}",
],
[
'shouldVerify' => $this->options['verify_batch_events_request'] ?? true,
]
)->getResponse();
}
}