tm4b/tm4b-php

PHP Client for using TM4B's REST API

v1.0.4 2021-02-27 15:10 UTC

This package is auto-updated.

Last update: 2024-03-27 21:52:02 UTC


README

687474703a2f2f7777772e746d34622e636f6d2f6173736574732f696d672f6c6f676f2d77686974652d6f6e2d626c75652e706e67 Build Status

Sign up for a TM4B account and visit our Developer Api for even more content.

TM4B PHP Client

This is the official PHP library for using the TM4B REST API. This SDK contains methods for easily interacting with the TM4B Messaging API. Below are examples to get you started. For additional examples, please see our official documentation at https://www.tm4b.com/en/sms-api/

Installation

The recommended way to install TM4B PHP is through Composer. Require the tm4b/tm4b-php package:

$ composer require tm4b/tm4b-php

Usage

Basic usage

You should always use Composer's autoloader in your application to automatically load your dependencies. All examples below assumes you've already included this in your file:

require 'vendor/autoload.php';

Initialize your TM4B Client with your TM4B API Key:

<?php

require 'vendor/autoload.php';

$msgClient = \Tm4b\Rest\Client::create([
    'apiKey' => 'TM4B_API_KEY'
]);

Quick Start

Here is a quick example:

POST /api/rest/v1/sms

<?php

require 'vendor/autoload.php';

$msgClient = \Tm4b\Rest\Client::create([
    'apiKey' => 'TM4B_API_KEY'
]);

try {
    $response = $msgClient->messages()->send([
        'destination_address' => '+441234567890',
        'source_address'      => 'master',
        'content'             => 'There is a civil war between the Galactic Empire and a Rebel Alliance.'
    ]);
    print_r($response);
} catch (\Tm4b\Exception\HttpClientException $e) {
    print_r($e->getResponseBody());
}

Examples

Bulk SMS

Let's send the same SMS to 2 recipients.

<?php

require 'vendor/autoload.php';

$msgClient = \Tm4b\Rest\Client::create([
    'apiKey' => 'TM4B_API_KEY'
]);

$response = $msgClient->messages()->send([
    [
        'destination_address' => '+447711961111',
        'source_address'      => 'GeorgeLucas',
        'content'             => 'There is a civil war between the Galactic Empire and a Rebel Alliance.'
    ],
    [
        'destination_address' => '+97150349030',
        'source_address'      => 'GeorgeLucas',
        'content'             => 'There is a civil war between the Galactic Empire and a Rebel Alliance.'
    ]
]);

Sandbox

<?php

require 'vendor/autoload.php';

$msgClient = \Tm4b\Rest\Client::create([
    'apiKey' => 'TM4B_API_KEY'
]);

$msgClient->setSandbox(true);
$response = $msgClient->messages()->send([
    [
        'destination_address' => '+447711961111',
        'source_address'      => 'GeorgeLucas',
        'content'             => 'There is a civil war between the Galactic Empire and a Rebel Alliance.'
    ]
]);

Account

<?php

require 'vendor/autoload.php';

$msgClient = \Tm4b\Rest\Client::create([
    'apiKey' => 'TM4B_API_KEY'
]);
$response = $msgClient->account()->describe();

General Usage

<?php

require 'vendor/autoload.php';

$msgClient = \Tm4b\Rest\Client::create([
    'apiKey' => 'TM4B_API_KEY'
]);

try {
    $response = $msgClient->get('/account');
    $data = $msgClient->hydrate($response, null);
    print_r($data);
} catch (\Http\Client\Exception $e) {
    print_r($e->getTraceAsString());
}