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(
    specversion: '1.0',
    type: 'user.created',
    source: 'user-service',
    subject: 'user-123',
    id: uniqid(),
    time: date('c'),
    datacontenttype: 'application/json',
    data: [
        'userId' => '123',
        'email' => '[email protected]',
        'name' => 'John Doe'
    ]
);

$eventArray = $event->toArray();
// Array with all CloudEvent fields

$eventData = [
    'specversion' => '1.0',
    'type' => 'user.created',
    'source' => '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);

try {
    $event->validate();
    echo "Event is valid!";
} catch (InvalidArgumentException $e) {
    echo "Event validation failed: " . $e->getMessage();
}
bash
composer