Skip to content

SocketBee PHP SDK

This PHP SDK allows you to send SocketBee events in PHP.

Installation

You can install the package via Composer. Run the following command in your project's root directory:

bash
composer require socketbee/socketbee

Usage

First, you need to import the SocketBee class at the top of your PHP file:

php
use SocketBee\SocketBee;

Next, create an instance of the SocketBee class by providing your app_id, secret, and key. You can also pass an optional config array to customize the default settings.

php
$socketBee = new SocketBee($app_id, $secret, $key, $config);

Sending Events

To send an event, use the sendEvent method and provide the channel, event name, and data (optional):

php
$response = $socketBee->sendEvent($channel, $event, $data);

The sendEvent method returns an array with the following structure:

php
[
    'status' => $statusCode, // HTTP status code of the response
    'body' => $responseBody, // Response body (in case of success)
    'errors' => $errorBody, // Error message (in case of failure)
    'error_message' => $errorMessage, // Error message (in case of failure without a response)
]

Example

Here's a complete example of how to use the SocketBee class:

php
use SocketBee\SocketBee;

$app_id = 'your_app_id';
$secret = 'your_secret';
$key = 'your_key';

$socketBee = new SocketBee($app_id, $secret, $key);

$channel = 'your_channel';
$event = 'your_event';
$data = [
    'message' => 'Hello, SocketBee!',
    'timestamp' => time(),
];

$response = $socketBee->sendEvent($channel, $event, $data);

if ($response['status'] === 200) {
    echo "Event sent successfully: " . $response['body'];
} else {
    echo "Error sending event: " . ($response['errors'] ?? $response['error_message']);
}

Make sure to replace your_app_id, your_secret, your_key, your_channel, and your_event with your actual values.

You can find your app credentials here on the dashboard.

Configuration

The SocketBee class accepts an optional config array as the fourth argument in the constructor. This array can be used to customize the default settings. The available options are:

  • protocol (default: 'https'): The protocol to use for the API requests.
  • host (default: 'east-1.socket.socketbee.com'): The host for the API requests.
  • port (default: 443): The port for the API requests.

Example:

php
$config = [
    'protocol' => 'https',
    'host' => 'your.custom.host',
    'port' => 8080,
];

$socketBee = new SocketBee($app_id, $secret, $key, $config);

That's it! You should now be able to install and use the socketbee/socketbee package in your PHP projects. If you have any further questions or issues, please contact us, we will be happy to help.