This example sends one event to LogZuno's ingestion API from PHP. It is the exact file our snippet tests send to the API before the docs are published.
Requirements
PHP 8 with the curl and json extensions (both are in most installations).
Send an event
<?php
// PHP 8 with the curl extension.
$endpoint = getenv('LOGZUNO_ENDPOINT') ?: 'https://api.logzuno.app/v1';
$ch = curl_init("$endpoint/log");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer '.getenv('LOGZUNO_API_KEY'),
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode([
'channel' => 'signups',
'event' => 'User signed up',
'user_id' => 'user-123',
'icon' => '🎉',
'notify' => true,
'tags' => ['plan' => 'growth', 'source' => 'website'],
]),
]);
$body = curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_RESPONSE_CODE), ' ', $body, PHP_EOL;Export LOGZUNO_API_KEY and run php logzuno.php. LogZuno answers 202 and the event appears in your project's live feed within seconds. Keep the key in an environment variable or your secrets manager, not in source control.
Notes for PHP
Send after the response. Under PHP-FPM, call fastcgi_finish_request() before sending, or send from a queued job, so your page is never slower because of analytics.
Laravel. Put the key in .env and read it with config(), and send events from a queued job with Laravel's HTTP client. The request body and headers are the same as above.
Errors. curl_exec returns the body for 4xx responses too: check the status code and log LogZuno's message, which names the field that was wrong.
What the request contains
The body is the same in every language: a channel and an event, and optionally user_id, group_id, description, icon, notify and tags. All fields, limits and response codes are in sending events and the ingestion API reference.