Skip to content

Code Examples

Quick Ones

Since sending SMS’es is a central part of most customers’ use cases we’ll list the code examples in full.

Quick Start

<?php
// Query args
$query = http_build_query(array(
    'token' => ‘API-token',
    'sender' => 'ExampleSMS',
    'message' => 'Hello World',
    'recipients.0.msisdn' => 980000000001,
));
// Send it
$result = file_get_contents('https://sms.mavorion.com/v2/mtsms?' . $query);
// Get SMS ids (optional)
print_r(json_decode($result)->ids);
?>

API Tokens and the support for form data is a great match for cURL integration, since sending an SMS becomes as easy as:

curl -v https://sms.mavorion.com/v2/mtsms \
    -u API-token: \
    -d sender="ExampleSMS" \
    -d message="Hello World" \
    -d recipients.0.msisdn=9800000001

Production Examples

The above example is good for trying to get a quick sms through to your number as a test, but is not recommened for production use, you should consider the below examples, using composer or cURL.

Outgoing SMS Example

<?php
$recipients = ['9800000001', '9800000002'];
$url = "https://sms.mavorion.com/v2/mtsms";
$api_token = "Go-Create-An-API-token";
$json = [
    'sender' => 'ExampleSMS',
    'message' => 'Hello world',
    'recipients' => [],
];

foreach ($recipients as $msisdn) {
    $json['recipients'][] = ['msisdn' => $msisdn];
}

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch,CURLOPT_USERPWD, $api_token.":");
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($json));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

print($result);                    // print result as json string
$json = json_decode($result);      // convert to object
print_r($json->ids);               // print the array with ids
print_r($json->usage->total_cost); // print total cost from ‘usage’ object