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

Clickatell One API: Read the account balance and currency

See more Clickatell Examples

Request the messaging balance associated with a One API integration key. The response supplies both the decimal amount and its currency. Read the amount as text for display to preserve its precision. A zero balance is a valid result. For example, a billing dashboard can display 1267.92865 ZAR.

Clickatell API reference

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;

$success = false;

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

$resp = new HttpResponse();
try {
    $http->HttpNoBody('GET', 'https://platform.clickatell.com/v1/balance', $resp);
    $success = true;
} catch (\Chilkat2\Exception $e) {
    $success = false;
}
if ($resp->StatusCode == 0) {
    echo "No HTTP response. Check the connection and retry the lookup.\n";
    exit;
}

$reply = new JsonObject();
try {
    $reply->Load($resp->BodyStr);
    $parsed = true;
} catch (\Chilkat2\Exception $e) {
    $parsed = false;
}
if ((!$success) || (($resp->StatusCode != 200))) {
    echo 'Clickatell returned HTTP ', $resp->StatusCode, "\n";
    if ($parsed) {
        if ($reply->HasMember('error.description')) {
            echo $reply->StringOf('error.description'), "\n";
        }
    }

    exit;
}

if (!$parsed) {
    echo "Expected a JSON response.\n";
    exit;
}

// JsonTypeOf returns 2 for a number and 1 for a string. Zero is a valid amount.
if (($reply->JsonTypeOf('balance') != 2) || ($reply->JsonTypeOf('currency') != 1)) {
    echo "The balance response is missing the amount or currency.\n";
    exit;
}

echo 'Account balance: ', $reply->StringOf('balance'), ' ', $reply->StringOf('currency'), "\n";