Clickatell offers quite a few API’s to help developers integrate with their SMS gateway. The only problem I found, was that the different protocols tend to behave differently.

I created this PHP library to make it easier to integrate and easily switch protocols (types of API’s). Using this library you can easily go from using the HTTP API to using the XML API (or other) with almost no code modification required.

1. Installation

This library uses composer and can be acquired using the following in your composer.json file.

1 <?php
2 {
3     "require": {
4         "arcturial/clickatell": "*"
5     }
6 }

2. Usage

The Clickatell library allows you specify several ways to connect to Clickatell. The current ones supported are HTTP and XML. These connections are called ‘Transports’. The default transport is HTTP.

1 <?php
2 $clickatell = new Clickatell($username, $password, $apiID);
3 $clickatell->sendMessage(array(1111111111), 'My Message');

You can specify a different output using the Clickatell constructor or using the setTransport() method.

 1 <?php
 2 $clickatell = new Clickatell(
 3     $username,
 4     password,
 5     $apiID,
 6     Clickatell::TRANSPORT_XML
 7 );
 8 
 9 // OR
10 $clickatell = new Clickatell($username, $password, $apiID);
11 $clickatell->setTransport(new ClickatellComponentTransportTransportXml);

NOTE: The library uses name spaces, and the Clickatell messenger is located at ClickatellClickatell

3. Supported API calls

Clickatell has a couple of different API’s that each support a subset of functions. We are going to refer to them as Messaging and Bulk Messaging API’s for this document.

Messaging API’s

The following are all messaging API’s.

ClickatellComponentTransportTransportHttp

ClickatellComponentTransportTransportSoap

ClickatellComponentTransportTransportXml

ClickatellComponentTransportTransportSmtp

These Transports all support the following functions

1 <?php
2 sendMessage(array $to, string $message, $from = "", $callback = true);
3 getBalance();
4 queryMessage($apiMsgId);
5 routeCoverage($msisdn);
6 getMessageCharge($apiMsgId);

Bulk Messaging API’s

The following are bulk messaging API’s. The have only a limited number of functions and are more suited for bulk messaging. Since they aren’t processed in real time, these Transports do not return the same results as the normal messaging API’s.

ClickatellComponentTransportTransportSMTP

These Transports all support the following functions

1 <?php
2 sendMessage(array $to, string $message, $from = "", $callback = true);

4. Events

This library provides a couple of events to extend the ability of the API’s. Current support events are request and response.

Example:

 1 <?php
 2 use ClickatellClickatell;
 3 
 4 $clickatell = new Clickatell('[username]', '[password]', [api_id], Clickatell::HTTP_API);
 5 
 6 $clickatell->on('request', function($data) {
 7     // $data = The parameters passed to the request
 8     print_r($data);
 9 });
10 
11 $clickatell->on('response', function($data) {
12     // $data = The result of the API call.
13 
14     // This hook can be used to register multiple
15     // listeners that can log to file/db or call another
16     // service.
17     print_r($data);
18 });