Sign up for a TM4B account and visit our Developer Api for even more content.
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/
The recommended way to install TM4B PHP is through
Composer. Require the tm4b/tm4b-php
package:
$ composer require tm4b/tm4b-php
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'
]);
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());
}
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.'
]
]);
<?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.'
]
]);
<?php
require 'vendor/autoload.php';
$msgClient = \Tm4b\Rest\Client::create([
'apiKey' => 'TM4B_API_KEY'
]);
$response = $msgClient->account()->describe();
<?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());
}