botika/socket

Library for interacting with the Botika Socket REST API

v1.0.8 2022-09-12 06:52 UTC

This package is auto-updated.

Last update: 2024-12-12 12:06:46 UTC


README

PHP library for interacting with the Botika Socket HTTP API.

Installation

You can get the Botika Socket PHP library via a composer package called socket. See https://packagist.org/packages/botika/socket

composer require botika/socket

Or add to composer.json:

"require": {
    "botika/socket": "^1.0"
}

then run composer update.

Supported platforms

  • PHP - supports PHP versions 7.0, and above.

Botika Socket constructor

Use the credentials from your Botika Socket application to create a new Botika\Socket instance.

$baseURL = 'https://socket.example.com';
$username = 'USERNAME';
$password = 'PASSWORD';
$auth = new \Botika\Socket\Auth($username, $password);

// Initialize socket
$socket = new \Botika\Socket\Socket($baseURL, $auth);

Logging configuration

The recommended approach of logging is to use a PSR-3 compliant logger implementing Psr\Log\LoggerInterface. The Socket object implements Psr\Log\LoggerAwareInterface, meaning you call setLogger(LoggerInterface $logger) to set the logger instance.

// where $logger implements `LoggerInterface`

$socket->setLogger($logger);

Publishing/Triggering events

To trigger an event on one or more channels use the trigger function.

A single channel

// Options get from https://docs.guzzlephp.org/en/stable/request-options.html
$options = [];
$socket->trigger('my-channel', 'my_event', 'hello world', $options);

Multiple channels

// Options get from https://docs.guzzlephp.org/en/stable/request-options.html
$options = [];
$socket->trigger([ 'channel-1', 'channel-2' ], 'my_event', 'hello world', $options);

Asynchronous interface

Both trigger have asynchronous counterparts in triggerAsync. These functions return Guzzle promises which can be chained with ->then:

// Options get from https://docs.guzzlephp.org/en/stable/request-options.html
$options = [];
$promise = $socket->triggerAsync(['channel-1', 'channel-2'], 'my_event', 'hello world', $options);
$promise->then(
    function (ResponseInterface $res) {
        echo $res->getStatusCode() . "\n";
    },
    function (RequestException $e) {
        echo $e->getMessage() . "\n";
        echo $e->getRequest()->getMethod();
    }
);
$promise->wait();