ASPSMS WebAPI – PHP Code Examples

Practical PHP examples for the ASPSMS WebAPI (https://webapi.aspsms.com). Structure matches the WebAPI overview.

Contents
Requirement: PHP 7.4+ with cURL extension

1. Sending SMS

Check credentials

<?php
function checkCredentials(string $userkey, string $password): string {
    $url = 'https://webapi.aspsms.com/CheckUserCredentials?' . http_build_query([
        'Userkey' => $userkey, 'Password' => $password
    ]);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $r = curl_exec($ch);
    curl_close($ch);
    return $r;
}
?>

Credit balance

<?php
function getCredits(string $userkey, string $password): string {
    $url = 'https://webapi.aspsms.com/ASPSMSCredits?' . http_build_query([
        'Userkey' => $userkey, 'Password' => $password
    ]);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $r = curl_exec($ch); curl_close($ch);
    return $r;
}
?>

Estimate credits

<?php
function estimateCredits(string $userkey, string $password, array $msisdns): string {
    $url = 'https://webapi.aspsms.com/ASPSMSEstimateCreditConsumption?' . http_build_query([
        'Userkey' => $userkey, 'Password' => $password
    ]);
    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true,
        CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
        CURLOPT_POSTFIELDS => json_encode($msisdns)
    ]);
    $r = curl_exec($ch); curl_close($ch);
    return $r;
}
?>

Simple SMS (GET)

<?php
function sendSimpleSms(string $userkey, string $password, string $msisdn, string $message, string $originator): string {
    $url = 'https://webapi.aspsms.com/SendSimpleSMS?' . http_build_query([
        'Userkey' => $userkey, 'Password' => $password, 'MSISDN' => $msisdn,
        'Operation' => 'SendTextSMS', 'MessageData' => $message, 'Originator' => $originator
    ]);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $r = curl_exec($ch); curl_close($ch);
    return $r;
}
?>

Advanced SMS (POST)

<?php
function sendAdvancedSms(string $userkey, string $password, string $message, string $originator, array $recipients): string {
    $url = 'https://webapi.aspsms.com/ASPSMSSendSMS?' . http_build_query(['Userkey' => $userkey, 'Password' => $password]);
    $payload = ['Operation' => 'SendTextSMS', 'Originator' => $originator, 'MessageData' => $message, 'Recipients' => $recipients];
    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true,
        CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
        CURLOPT_POSTFIELDS => json_encode($payload)
    ]);
    $r = curl_exec($ch); curl_close($ch);
    return $r;
}
?>

Personalized SMS with RecipientsEx

Same originator for all, individual text (and optional TRN / deferred time) per recipient.

<?php
function sendPersonalizedSms(string $userkey, string $password, string $originator, array $messages): string {
    // $messages e.g.:
    // [
    //   ['PhoneNumber' => '+41791111111', 'TransactionReferenceNumber' => 'ORDER-1001',
    //    'MessageData' => 'Hi Anna, your order #1001 is on the way.'],
    //   ['PhoneNumber' => '+41792222222', 'TransactionReferenceNumber' => 'ORDER-1002',
    //    'MessageData' => 'Hi Bruno, your order #1002 is on the way.',
    //    'DeferredDeliveryTime' => '2026-10-20T08:00:00']  // optional per recipient
    // ]
    $url = 'https://webapi.aspsms.com/ASPSMSSendSMS?' . http_build_query(['Userkey' => $userkey, 'Password' => $password]);
    $payload = [
        'Operation' => 'SendTextSMS',
        'Originator' => $originator,   // same for all
        'RecipientsEx' => $messages    // individual MessageData per recipient
    ];
    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true,
        CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
        CURLOPT_POSTFIELDS => json_encode($payload)
    ]);
    $r = curl_exec($ch); curl_close($ch);
    return $r;
}
?>

Deactivate pending SMS

<?php
function deactivateByMsisdn(string $userkey, string $password, array $msisdns): string {
    $url = 'https://webapi.aspsms.com/ASPSMSDeactivatePendingSMSWithMSISDN?' . http_build_query(['Userkey' => $userkey, 'Password' => $password]);
    $ch = curl_init($url);
    curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true,
        CURLOPT_HTTPHEADER => ['Content-Type: application/json'], CURLOPT_POSTFIELDS => json_encode($msisdns)]);
    $r = curl_exec($ch); curl_close($ch);
    return $r;
}
?>

2. Delivery Webhooks

Webhooks are called exclusively via HTTP GET. Placeholders: <SCTS>, <DSCTS>, <RSN>, <DST>, <TRN>, <RCPNT>, <CREDITS> (max. 200 chars per URL).
<?php
function sendSmsWithWebhooks(string $userkey, string $password, string $message, string $originator, array $recipients): string {
    $url = 'https://webapi.aspsms.com/ASPSMSSendSMS?' . http_build_query(['Userkey' => $userkey, 'Password' => $password]);
    $payload = [
        'Operation' => 'SendTextSMS', 'Originator' => $originator, 'MessageData' => $message, 'Recipients' => $recipients,
        'URLBufferedMessageNotification' => 'https://your-domain.com/hook/buffered?SCTS=<SCTS>&DSCTS=<DSCTS>&RSN=<RSN>&DST=<DST>&TRN=<TRN>&RCPNT=<RCPNT>',
        'URLDeliveryNotification' => 'https://your-domain.com/hook/delivered?SCTS=<SCTS>&DSCTS=<DSCTS>&RSN=<RSN>&DST=<DST>&TRN=<TRN>&RCPNT=<RCPNT>',
        'URLNonDeliveryNotification' => 'https://your-domain.com/hook/failed?SCTS=<SCTS>&DSCTS=<DSCTS>&RSN=<RSN>&DST=<DST>&TRN=<TRN>&RCPNT=<RCPNT>'
    ];
    $ch = curl_init($url);
    curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true,
        CURLOPT_HTTPHEADER => ['Content-Type: application/json'], CURLOPT_POSTFIELDS => json_encode($payload)]);
    $r = curl_exec($ch); curl_close($ch);
    return $r;
}
?>

Delivery Status (DST)

ValueMeaning
-1Not yet submitted / rejected
0Delivered
1Buffered
2Not Delivered

After status 1 (Buffered) you almost always get a second call with 0 or 2.

Common Reason Codes (RSN)

CodeMeaningTypical cause
000Unknown SubscriberNumber does not exist
103Call BarredBlocked / empty prepaid
107Absent SubscriberPhone off / no network
108Delivery FailMemory full or final failure
110Protocol ErrorDevice problems
118System failSevere network/device issues

3. Sendlog, Statistics & Two-Way

Daily traffic

<?php
function getTrafficStat(string $userkey, string $password, bool $calculateCredits = true): string {
    $url = 'https://webapi.aspsms.com/TrafficStat01/Get?' . http_build_query([
        'Userkey' => $userkey, 'Password' => $password, 'CalculateCredits' => $calculateCredits ? 'true' : 'false'
    ]);
    $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $r = curl_exec($ch); curl_close($ch); return $r;
}
?>

Send log by date

<?php
function getSendingLog(string $userkey, string $password, int $year, int $month = 0, int $day = 0): string {
    $url = 'https://webapi.aspsms.com/SendingStat01/Get?' . http_build_query([
        'Userkey' => $userkey, 'Password' => $password, 'Year' => $year, 'Month' => $month, 'Day' => $day
    ]);
    $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $r = curl_exec($ch); curl_close($ch); return $r;
}
?>

Send log by MSISDN / TRN / JobID

<?php
// SendingStat02 (MSISDN), SendingStat03 (TRN), SendingStat04 (JobID) – same GET pattern
function getLogByTrn(string $userkey, string $password, string $trn): string {
    $url = 'https://webapi.aspsms.com/SendingStat03/Get?' . http_build_query([
        'Userkey' => $userkey, 'Password' => $password, 'TransactionReferenceNumber' => $trn
    ]);
    $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $r = curl_exec($ch); curl_close($ch); return $r;
}
?>

Inbox (received SMS)

<?php
function getReceivedSms(string $userkey, string $password, int $year = 0, int $month = 0, int $day = 0): string {
    $url = 'https://webapi.aspsms.com/ReceivingStat01/Get?' . http_build_query([
        'Userkey' => $userkey, 'Password' => $password, 'Year' => $year, 'Month' => $month, 'Day' => $day, 'MaxRecords' => 100
    ]);
    $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $r = curl_exec($ch); curl_close($ch); return $r;
}
?>

Receive URL (push for inbound SMS)

<?php
function setReceiveUrl(string $userkey, string $password, string $callbackUrl): string {
    $url = 'https://webapi.aspsms.com/ReceiveURL/Set?' . http_build_query([
        'Userkey' => $userkey, 'Password' => $password, 'URL' => $callbackUrl
    ]);
    $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $r = curl_exec($ch); curl_close($ch); return $r;
}
?>

4. Delivery Statistics per Recipient

<?php
function getMsisdnDlrStat(string $userkey, string $password): string {
    $url = 'https://webapi.aspsms.com/MSISDNDLRStat/Get?' . http_build_query(['Userkey' => $userkey, 'Password' => $password]);
    $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $r = curl_exec($ch); curl_close($ch); return $r;
}
?>

5. Phonebook

5.1 Phone numbers

<?php
function getPhonebook(string $userkey, string $password, ?string $msisdn = null): string {
    $params = ['Userkey' => $userkey, 'Password' => $password];
    if ($msisdn) $params['MSISDN'] = $msisdn;
    $url = 'https://webapi.aspsms.com/PhoneNumbers/Read?' . http_build_query($params);
    $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $r = curl_exec($ch); curl_close($ch); return $r;
}
?>

5.2 Groups

<?php
function getGroups(string $userkey, string $password): string {
    $url = 'https://webapi.aspsms.com/Groups/Get?' . http_build_query(['Userkey' => $userkey, 'Password' => $password]);
    $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $r = curl_exec($ch); curl_close($ch); return $r;
}
?>

5.3 Group relations

<?php
function getAllGroupRelations(string $userkey, string $password): string {
    $url = 'https://webapi.aspsms.com/GroupRelationsAll/Get?' . http_build_query(['Userkey' => $userkey, 'Password' => $password]);
    $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $r = curl_exec($ch); curl_close($ch); return $r;
}
?>

6. Jobs

Document sending actions. Pass JobID when sending; query messages later via SendingStat04.

<?php
function getJobs(string $userkey, string $password): string {
    $url = 'https://webapi.aspsms.com/Jobs/Get?' . http_build_query(['Userkey' => $userkey, 'Password' => $password]);
    $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $r = curl_exec($ch); curl_close($ch); return $r;
}
?>

7. Templates

<?php
function getTemplates(string $userkey, string $password): string {
    $url = 'https://webapi.aspsms.com/Templates/Get?' . http_build_query(['Userkey' => $userkey, 'Password' => $password]);
    $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $r = curl_exec($ch); curl_close($ch); return $r;
}
?>