PHP code example of quinluong / miczone-wrapper-php

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

    

quinluong / miczone-wrapper-php example snippets


use Miczone\Wrapper\MiczoneEventBusClient\Notifier;
use Miczone\Thrift\EventBus\NotifyMessageResponse;

$client = new Notifier([
    'hosts' => '127.0.0.1:1234',
    'clientId' => 'my-service',
    'auth' => 'user:pass',
    'topics' => 'topicA,topicB',
    'numberOfRetries' => 3 // optional
]);

// Async
$client->ow_notifyString([
    'topic' => 'topicA',
    'key' => 'my-key', // to keep packages in order if they have the same key
    'data' => '{}' // data type can be Boolean / Integer / Double / String
], function () {
    // Success callback
}, function ($ex) {
    // Error callback
});

// Sync, type of $response is NotifyMessageResponse
$response = $client->notifyString([
    'topic' => 'topicB',
    'key' => 'my-key',
    'data' => '{}',
]);

use Miczone\Wrapper\MiczoneEventBusClient\Watcher;
use Miczone\Wrapper\MiczoneEventBusClient\WatcherHandler\StringWatcherHandlerInterface;

$client = new Watcher([
    'hosts' => '127.0.0.1:1234',
    'clientId' => 'my-service',
    'groupId' => 'my-group',
    'topics' => 'topicA,topicB'
]);


/**
 * Handler interface can be:
 * - BooleanWatcherHandlerInterface
 * - IntegerWatcherHandlerInterface
 * - DoubleWatcherHandlerInterface
 * - StringWatcherHandlerInterface
 */
$client->setHandler(new class implements StringWatcherHandlerInterface {
    public function onMessage(string $topic, int $partition, int $offset, int $timestamp, string $key = null, string $data = null) {
        // Do your stuff
    }

    public function onError($code, string $message, \Exception $exception = null) {
        // Catch error
    }
});

$client->start();
bash
composer