Skip to main content

Relay SDK for PHP

Getting Started

The Relay SDK for PHP enables PHP developers to connect and use SignalWire's Relay APIs within their own PHP code. Our Relay SDK allows developers to build or add robust and innovative communication services to their applications.

The Relay SDK for PHP is easy to use and only takes a few minute to setup and get running.

Latest Version:
Source Code: signalwire-community/signalwire-php

Installation

Install the package using Composer:

composer require signalwire-community/signalwire

Minimum Requirements

The PHP SDK requires PHP 8.0 or greater installed on your system.

Using the SDK

The PHP SDK can be used to get up and running with Relay quickly and easily. In order to use the PHP client, you must get your project and token from your SignalWire dashboard.

There are a few ways to get started, depending on your needs: Relay.Consumer, Relay.Task, and Relay.Client.

Relay Consumer

A Relay.Consumer creates a long running process, allowing you to respond to incoming requests and events in realtime. Relay Consumers abstract all the setup of connecting to Relay and automatically dispatches workers to handle requests; so you can concentrate on writing your code without having to worry about multi-threading or blocking, everything just works. Think of Relay Consumers like a background worker system for all your calling and messaging needs.

Relay Consumers can scale easily, simply by running multiple instances of your Relay.Consumer process. Each event will only be delivered to a single consumer, so as your volume increases, just scale up! This process works well whether you are using Docker Swarm, a Procfile on Heroku, your own webserver, and most other environments.

Setting up a new consumer is the easiest way to get up and running.
<?php

require dirname(__FILE__) . '/vendor/autoload.php';

use Generator as Coroutine;
use SignalWire\Relay\Consumer;

class CustomConsumer extends Consumer {
public $project = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX';
public $token = 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
public $contexts = ['home', 'office'];

public function ready(): Coroutine {
yield;
// Consumer is successfully connected with Relay.
// You can make calls or send messages here..
}

public function onIncomingCall($call): Coroutine {
$result = yield $call->answer();
if ($result->isSuccessful()) {
yield $call->playTTS(['text' => 'Welcome to SignalWire!']);
}
}
}

$consumer = new CustomConsumer();
$consumer->run();

Learn more about Relay Consumers

Relay Task

A Relay.Task is simple way to send jobs to your Relay.Consumers from a short lived process, like a web framework. Relay Tasks allow you to pass commands down to your Consumers without blocking your short lived request. Think of a Relay Task as a way to queue a job for your background workers to processes asynchronously.

For example, if you wanted to make an outbound call and play a message when your user clicks a button on your web application, since Relay is a realtime protocol and relies on you to tell it what to do in realtime, if you did this within your web application, your web server would block until the call was finished... this may take a long time! Instead, simply create a new Relay Task. This task will be handled by a running Relay Consumer process and your web application can respond back to your user immediately.

Send a task in the 'office' context with custom data.
// create-task.php
<?php

require dirname(__FILE__) . '/vendor/autoload.php';

use SignalWire\Relay\Tasking\Task;

$task = new Task('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
$context = 'office';
$delivered = $task->deliver($context, [
'uuid' => 'unique id',
'data' => 'data for your job'
]);
Handle the task in a Consumer.
// consumer-task.php
<?php

require dirname(__FILE__) . '/vendor/autoload.php';

use Generator as Coroutine;

class CustomConsumer extends SignalWire\Relay\Consumer {
public $project = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX';
public $token = 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
public $contexts = ['office']; // Task is delivered on the "office" context!

public function onTask($message): Coroutine {
yield;
// Retrieve your custom properties in $message..
echo $message->uuid; // 'unique id'
echo $message->data; // 'data for your job'
}
}

$consumer = new CustomConsumer();
$consumer->run();

Learn more about Relay Tasks

Relay Client

Relay.Client is a lower level object, giving you a basic connection to Relay but that is all. It is best used when you are creating a script only concerned with sending outbound requests or you want complete control over the Relay connection yourself.

Setting up a new client and make an outbound call.
<?php

$client = new SignalWire\Relay\Client([
'project' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'token' => 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
]);

$client->on('signalwire.ready', function($client) {

$params = [ 'type' => 'phone', 'from' => '+1XXXXXXXXXX', 'to' => '+1YYYYYYYYYY' ];
$client->calling->dial($params)->done(function($result) {
if ($result->isSuccessful()) {
// Your active $call..
$call = $result->getCall();
}
});

});

$client->connect();

Learn more about Relay Clients

Contexts

Relay uses Contexts as a simple way to separate events to specific consumers, allowing you to write consumers for specific types of calls or messages or scale them independently. A Context is simply a named string, that allows you to categorize requests. When creating outbound requests, or configuring phone numbers for inbound requests, you can specify the context; Relay will then deliver that call or event to Consumers that are configured to listen for that context.

For example, you could have a customer support phone number configured to send to Relay with the support context, and a personal number configured with personal context. Relay would deliver these events to any Consumer listening for those contexts. This gives you a lot of control in how messages are delivered to your Consumers, allowing you to write Consumer classes specific to the context, scale them independently, or separate traffic based on your own business rules.