Using Twilio PHP SDK

If you are using the Twilio PHP SDK, you can swap out the URL to point to the MessageBird Twilio Adapter, by using a custom HTTP Client.

<?php

class MyTwilioAdapterClient extends Twilio\Http\CurlClient
{

    public function options(
        string $method,
        string $url,
        array $params = [],
        array $data = [],
        array $headers = [],
        string $user = null,
        string $password = null,
        int $timeout = null
    ): array {
        $adapterUrl = str_replace("https://api.twilio.com/", "https://eu-west-1.twilio.to.nest.messagebird.com/", $url);

        return parent::options($method, $adapterUrl, $params, $data, $headers, $user, $password, $timeout);
    }
}

// Your Account SID and Auth Token
$accountSid = "ACXXXXXX";
$authToken = "YYYYYY";

$httpClient = new MyTwilioAdapterClient();
$client = new Twilio\Rest\Client($accountSid, $authToken, null, null, $httpClient);

You can try it out with the following example:

<?php

// Required if your environment does not handle autoloading
require __DIR__ . '/vendor/autoload.php';

class MyTwilioAdapterClient extends Twilio\Http\CurlClient
{
    public function options(
        string $method,
        string $url,
        array $params = [],
        array $data = [],
        array $headers = [],
        string $user = null,
        string $password = null,
        int $timeout = null
    ): array {
        $adapterUrl = str_replace("https://api.twilio.com/", "https://eu-west-1.twilio.to.nest.messagebird.com/", $url);

        return parent::options($method, $adapterUrl, $params, $data, $headers, $user, $password, $timeout);
    }
}

// Your Account SID and Auth Token
$accountSid = "ACXXXXXX";
$authToken = "YYYYYY";

$httpClient = new MyTwilioAdapterClient();
$client = new Twilio\Rest\Client($accountSid, $authToken, null, null, $httpClient);

// Use the Client to make requests to the MessageBird Twilio Adapter
$client->messages->create(
    // The number you'd like to send the message to
    '+15558675309',
    [
        // A valid MessageBird phone number
        'from' => '+15017250604',
        // The body of the text message you'd like to send
        'body' => "Hey Jenny! Good luck on the bar exam!"
    ]
);

Last updated