PHP library for interacting with the Botika Socket HTTP API.
You can get the Botika Socket PHP library via a composer package called socket. See https://packagist.org/packages/botika/socket
composer require botika/socketOr add to composer.json:
"require": {
"botika/socket": "^1.0"
}then run composer update.
- PHP - supports PHP versions 7.0, and above.
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);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);To trigger an event on one or more channels use the trigger function.
// Options get from https://docs.guzzlephp.org/en/stable/request-options.html
$options = [];
$socket->trigger('my-channel', 'my_event', 'hello world', $options);// Options get from https://docs.guzzlephp.org/en/stable/request-options.html
$options = [];
$socket->trigger([ 'channel-1', 'channel-2' ], 'my_event', 'hello world', $options);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();