PHP Code Sample

HTTP API Code Sample – PHP

The code sample below shows you how to send SMS messages quickly using a simple PHP script based on the HTTP API. The script sends a single 1-way SMS message that will appear to come from "XYZCorp".


Send a 1-Way SMS Using PHP

<?php


// Declare the security credentials to use
$username = "my_username";
$password = "SecrEt12345";


// Set the attributes of the message to send
$message = "Hello World";
$type = "1-way";
$senderid = "XYZCorp";
$to = "61400000000";


// Build the URL to send the message to. Make sure the
// message text and Sender ID are URL encoded. You can
// use HTTP or HTTPS
$url = "http://api.directsms.com.au/s3/http/send_message?" .
"username=" . $username . "&" .
"password=" . $password . "&" .
"message=" . urlencode($message) . "&" .
"type=" . $type . "&" .
"senderid=" . urlencode($senderid) . "&" .
"to=" . $to;


// Send the request
$output = file($url);


// The response from the gateway is going to look like
// this:
// id: a4c5ad77ad6faf5aa55f66a
//
// In the event of an error, it will look like this:
// err: invalid login credentials
$result = explode(":", $output[0]);


if($result[0] == "id")
{
echo("Message sent\n");
}
else
{
echo("Error - " . $result[1] . "\n");
}


?>


Receive Inbound SMS Using PHP

The code sample below shows you how to process an SMS message that has just been received on one of your directSMS dedicated inbound SMS numbers using a simple PHP script based on the HTTP API:

<?php


// Get the different parameters that will be passed in
// when an SMS is received on a dedicated inbound number
$inbound_number = $_GET["inbound_number"]; //The receiving number
$message_id = $_GET["id"]; // The ID of this SMS @ directSMS
$mobile = $_GET["mobile"]; // The sending number
$message_text = $_GET["message_text"]; // Message text
$when = $_GET["when"]; // When this message arrived


// You may wish to log this message in a database
// or do some conditional processing based on the
// the sender or message content... Let's keep things
// simple right now and just send an email containing
// the important data
$email = "The following SMS message was received\n";
$email .= "\n";
$email .= "ID: " . $message_id . "\n";
$email .= "From: " . $mobile . "\n";
$email .= "To: " . $inbound_number . "\n";
$email .= "Message: " . $message_text . "\n";


// Send the email
mail("sms@example.com", "Inbound SMS Received",
$email, "From: sms@example.com");


?>


Process 2-Way Reply Using PHP

The code sample below shows you how to process a reply SMS that has when one of your clients or staff replies back to one of your outbound 2-way SMS messages using a simple PHP script based on directSMS’ HTTP API:

<?php


// Get the different parameters that will be passed in
// when a reply SMS is received


// 1. The ID of the original 2-Way SMS @ directSMS
$id = $_GET["id"];


// 2. The number of the person replying
$mobile = $_GET["mobile"];


// 3. The message text
$message_text = $_GET["message_text"];


// 4. How long ago this reply was received (in seconds)
$when = $_GET["when"]; // When this message arrived


// 5. The transaction ID (optional) passed by your system
// when you submitted the original 2-Way SMS for delivery
$transaction_no = $_GET["messageid"];


// You may wish to log this message in a database
// or do some conditional processing based on the
// the sender or message content... Let's keep things
// simple right now and just send an email containing
// the important data
$email = "The following 2-Way reply SMS message was received\n";
$email .= "\n";
$email .= "ID: " . $id . "\n";
$email .= "From: " . $mobile . "\n";
$email .= "Message: " . $message_text . "\n";
$email .= "Transaction No: " . $transaction_no . "\n";


// Send the email
mail("sms@example.com", "2-Way SMS Reply Received",
$email, "From: sms@example.com");


?>


The full HTTP/S API documentation details all available features of directSMS' Bulk SMS Gateway. For additional help or if you have any questions, please contact our helpful support team.