orus-io / php-xbus
PHP Client for xbus.io.
0.1.0
2019-10-17 17:02 UTC
Requires
- google/protobuf: ^3.6
- psr/log: ^1.0
- rmccue/requests: ^1.7
Requires (Dev)
- nats-rpc/nrpc: dev-master
- phpunit/phpunit: ^7
This package is auto-updated.
Last update: 2024-04-27 15:15:21 UTC
README
A PHP client for Xbus.
This client relies on the xbus-http gateway, and is limited to implementing emitters, consumers and workers.
Installation
composer require orus-io/php-xbus dev-default
Usage
Emit a message
To emit a simple envelope with one events containing one or more items:
<?php
$actor = new \XbusClient\Actor('http://test.test', 'emittername', 'theApiKey');
$actor->emitItems("a.event.type", "item1", "item2");
?>
Receive a message
Receiving messages rely on the webhook feature of xbus-http. The client application must provide a http endpoint, on which xbus-http will POST some data.
A typical implementation for the endpoint is:
<?php
$actor = new \XbusClient\Actor('http://test.test', 'consumername', 'theApiKey');
$actor->handleRequest(
$_SERVER['HTTP_CONTENT_TYPE'], // <- The incoming request content type
fopen('php://input', 'r'), // <- The incoming request body (as a resource)
'header', // <- a callable that set a header on the response
'http_response_code', // <- a callable that sets the http response code
fopen('php://output', 'w'), // The response body resource
// The user-defined handler. This example one will print the incoming items
function(\Xbus\ActorProcessRequest $request): ?\Xbus\ActorProcessingState {
foreach($request->getInputs() as $input) {
print "input: " . $input.getName();
foreach($input->getEnvelope()->getEvents() as $event) {
print "event: " . $event.getType();
foreach($event->getItems() as $item) {
print "item: " . $item;
}
};
};
}
);
?>