ASPSMS WebAPI – PHP Code-Beispiele

Praktische PHP-Beispiele fuer die ASPSMS WebAPI (https://webapi.aspsms.com). Die Gliederung entspricht der WebAPI-Uebersicht.

Inhalt
Voraussetzung: PHP 7.4+ with cURL extension

1. SMS senden

Credentials pruefen

<?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;
}
?>

Guthaben abfragen

<?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;
}
?>

Credits schaetzen

<?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;
}
?>

Einfache 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;
}
?>

Erweiterte 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;
}
?>

Personalisierte SMS mit RecipientsEx

Gleicher Absender fuer alle, individueller Text (und optional TRN / zeitversetzter Versand) pro Empfaenger.

<?php
function sendPersonalizedSms(string $userkey, string $password, string $originator, array $messages): string {
    // $messages z.B.:
    // [
    //   ['PhoneNumber' => '+41791111111', 'TransactionReferenceNumber' => 'ORDER-1001',
    //    'MessageData' => 'Hallo Anna, deine Bestellung #1001 ist unterwegs.'],
    //   ['PhoneNumber' => '+41792222222', 'TransactionReferenceNumber' => 'ORDER-1002',
    //    'MessageData' => 'Hallo Bruno, deine Bestellung #1002 ist unterwegs.',
    //    'DeferredDeliveryTime' => '2026-10-20T08:00:00']  // optional pro Empfaenger
    // ]
    $url = 'https://webapi.aspsms.com/ASPSMSSendSMS?' . http_build_query(['Userkey' => $userkey, 'Password' => $password]);
    $payload = [
        'Operation' => 'SendTextSMS',
        'Originator' => $originator,   // gleich fuer alle
        'RecipientsEx' => $messages    // individueller MessageData pro Empfaenger
    ];
    $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;
}
?>

Ausstehende SMS stornieren

<?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 werden ausschliesslich per HTTP GET aufgerufen. Platzhalter: <SCTS>, <DSCTS>, <RSN>, <DST>, <TRN>, <RCPNT>, <CREDITS> (max. 200 Zeichen pro 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)

WertBedeutung
-1Noch nicht eingereicht / abgelehnt
0Delivered
1Buffered
2Not Delivered

Nach Status 1 (Buffered) kommt fast immer ein zweiter Aufruf mit 0 oder 2.

Haeufige Reason Codes (RSN)

CodeBedeutungTypische Ursache
000Unknown SubscriberNummer existiert nicht
103Call BarredGesperrt / leere Prepaid-Karte
107Absent SubscriberHandy aus / kein Netz
108Delivery FailSpeicher voll oder endgueltig fehlgeschlagen
110Protocol ErrorGeraeteprobleme
118System failSchwere Netz-/Geraeteprobleme

3. Sendlog, Statistiken & Two-Way

Taeglicher 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;
}
?>

Sendlog nach Datum

<?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;
}
?>

Sendlog nach 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 (eingehende 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 fuer eingehende 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. Zustellstatistik pro Empfaenger

<?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. Telefonbuch

5.1 Telefonnummern

<?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 Gruppen

<?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 Gruppen-Zuordnungen

<?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

Versandaktionen dokumentieren. Beim Senden JobID mitgeben; spaeter per SendingStat04 abfragen.

<?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;
}
?>