PHP code example of psx / cloudevents

1. Go to this page and download the library: Download psx/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/ */

    

psx / cloudevents example snippets




$event = (new Builder())
    ->withType('com.example.someevent')
    ->withSource('/mycontext')
    ->withId('1234-1234-1234')
    ->withTime(new \DateTime('2018-04-05T17:31:00Z'))
    ->withDataContentType('application/json')
    ->withData(['foo' => 'bar'])
    ->withExtension('bar', 'foo')
    ->build();




$json = '{ ... }';
$event = Parser::parse(json_decode($json));

assert('1.0' === $event->getSpecVersion());
assert('com.example.someevent' === $event->getType());
assert('/mycontext' === $event->getSource());
assert('1234-1234-1234' === $event->getId());
assert($event->getTime() instanceof \DateTimeInterface::class);
assert('Thu, 05 Apr 2018 17:31:00 +0000' === $event->getTime()->format('r'));
assert('application/json' === $event->getDataContentType());
assert(['foo' => 'bar'] === $event->getData());
assert(['bar' => 'foo'] === $event->getExtensions());