tm4b / tm4b-php
PHP Client for using TM4B's REST API
Requires
- php: ^5.6 || ^7.0
- php-http/guzzle6-adapter: ^1.0 || ^2.0
- webmozart/assert: ^1.2
- zendframework/zend-diactoros: ^1.7
Requires (Dev)
- helmich/phpunit-psr7-assert: ^1.0
- php-http/mock-client: ^1.0
- phpspec/prophecy: ~1.0
- phpunit/phpunit: ^5.6
- vlucas/phpdotenv: ^2.4
README
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()); }