Sample code for 30+ languages & platforms
CkPhp Requires Chilkat v11.0.0+

Clickatell One API: Send an SMS and get its message ID

See more Clickatell Examples

Send one text using an SMS-enabled One API integration. Supply an international phone number starting with the country code, using digits only. For a US two-way integration, set from to its provisioned two-way number. With a test integration, use a registered test phone.

HTTP 202 means the submission was processed. Inspect accepted for each message. Save the returned apiMessageId with the application record and use it to look up delivery status. When a multi-message submission returns HTTP 207, check each result before retrying any rejected message.

Already using the SMS-only REST integration? Its documented request is POST /messages with the integration key in Authorization and a body such as {"content":"Test message","to":["PHONE_NUMBER"]}. One API has a different request body and a separate integration key. SMS API reference

Clickatell API reference

Chilkat CkPhp Downloads

CkPhp
<?php

include("chilkat.php");

$success = false;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
$http = new CkHttp();
$http->put_RequireSslCertVerify(true);
//  Supply the key for this One API integration from application configuration.
$http->SetRequestHeader('Authorization','ONE_API_KEY');
$http->SetRequestHeader('Accept','application/json');

$body = new CkJsonObject();
$body->UpdateString('messages[0].channel','sms');
$body->UpdateString('messages[0].to','PHONE_NUMBER');
$body->UpdateString('messages[0].content','Test message: your order is ready.');
//  For a two-way sender, supply the number provisioned on the integration.
$sender = '';
if ($sender != '') {
    $body->UpdateString('messages[0].from',$sender);
}

$resp = new CkHttpResponse();
$success = $http->HttpJson('POST','https://platform.clickatell.com/v1/message',$body,'application/json',$resp);
if ($resp->get_StatusCode() == 0) {
    print 'The send outcome is unknown. Check the message report before resending.' . "\n";
    exit;
}

$reply = new CkJsonObject();
$parsed = $reply->Load($resp->bodyStr());
if (($success != true) or (($resp->get_StatusCode() != 202) and ($resp->get_StatusCode() != 207))) {
    print 'Clickatell returned HTTP ' . $resp->get_StatusCode() . "\n";
    if ($parsed == true) {
        if ($reply->HasMember('error.description') == true) {
            print $reply->stringOf('error.description') . "\n";
        }

    }

    exit;
}

if ($parsed != true) {
    print 'Expected a JSON response.' . "\n";
    exit;
}

//  A request-level error applies to the submission as a whole.
if ($reply->HasMember('error.code') == true) {
    if ($reply->IsNullOf('error.code') != true) {
        print 'Submission error ' . $reply->stringOf('error.code') . '  ' . $reply->stringOf('error.description') . "\n";
        exit;
    }

}

$count = $reply->SizeOfArray('messages');
if ($count <= 0) {
    print 'No recipient results returned. Check the message report before resending.' . "\n";
    exit;
}

$i = 0;
while (($i < $count)) {
    $reply->put_I($i);
    $resultNumber = $i + 1;
    //  JsonTypeOf returns 5 for a JSON boolean.
    if ($reply->JsonTypeOf('messages[i].accepted') != 5) {
        print 'Result ' . $resultNumber . ' has no acceptance flag.' . "\n";
    }
    else {
        if ($reply->BoolOf('messages[i].accepted') == true) {
            $messageId = $reply->stringOf('messages[i].apiMessageId');
            if (($reply->JsonTypeOf('messages[i].apiMessageId') != 1) or ($messageId == '')) {
                print 'Result ' . $resultNumber . ' was accepted without a tracking ID.' . "\n";
            }
            else {
                print 'Result ' . $resultNumber . ' accepted. Message ID: ' . $messageId . "\n";
            }

        }
        else {
            print 'Result ' . $resultNumber . ' rejected: ' . $reply->stringOf('messages[i].error.code') . '  ' . $reply->stringOf('messages[i].error.description') . "\n";
        }

    }

    $i = $i + 1;
}


?>