PHP code example of utopia-php / cloudevents
1. Go to this page and download the library: Download utopia-php/cloudevents library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
utopia-php / cloudevents example snippets
topia\CloudEvents\CloudEvent;
$event = new CloudEvent(
type: 'com.example.user.created',
source: 'https://example.com/user-service',
id: uniqid(),
subject: 'user-123',
time: CloudEvent::now(),
data: [
'userId' => '123',
'email' => '[email protected] ',
'name' => 'John Doe'
]
);
$eventArray = $event->toArray();
// Array with all set CloudEvent attributes; absent optional attributes are omitted
$eventData = [
'specversion' => '1.0',
'type' => 'com.example.user.created',
'source' => 'https://example.com/user-service',
'subject' => 'user-123',
'id' => 'unique-id',
'time' => '2025-11-07T10:00:00Z',
'datacontenttype' => 'application/json',
'data' => [
'userId' => '123',
'email' => '[email protected] '
]
];
$event = CloudEvent::fromArray($eventData);
$json = $event->toJson();
$event = CloudEvent::fromJson($json);
$event = new CloudEvent(
type: 'com.example.user.created',
source: 'https://example.com/user-service',
id: uniqid(),
extensions: [
'traceparent' => '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01',
'sequence' => 42,
]
);
$event->extensions['traceparent'] ?? null; // '00-4bf9...'
$event->extensions; // all extension attributes
try {
$event->validate();
echo "Event is valid!";
} catch (InvalidArgumentException $e) {
echo "Event validation failed: " . $e->getMessage();
}
bash
composer