PHP API Documentation
PHP API Documentation
Release 2.1
User Manual
Introduction
The Messagemedia PHP API is designed to provide access to the Messagemedia SMS
server via a PHP interface. This access includes the sending and receiving of SMS
messages, as well as basic account maintenance.
The API is provided in the form of source code, which should be included in your
applications via the PHP command require("SmsInterface.inc")
Supported Platforms
Two versions of the API are provided, supporting different releases of PHP. To benefit
from the full feature set of the API, it is recommended that you use PHP 4.3 or
above.
SmsInterface-40.inc works with PHP 4.0 and above, but uses a number of
deprecated PHP commands, so it should only be used if you have a version of PHP
that is older than 4.2
SmsInterface.inc works with PHP 4.2 and above, and is the recommended version.
Note, however, that releases of PHP prior to 4.3 will not support encrypted
communications with the Messagemedia server via SSL, so it is recommended that
you upgrade to the 4.3 release to take full advantage of this API.
Because PHP is an interpreted language, PHP applications will run on any machine
that has an interpreter installed. PHP interpreters are available for Linux, Windows
NT, and all variants of Unix. For a complete list of the supported operating systems
and downloadable copies of their interpreters visit http://www.php.net
The API provides a wrapper for communicating with the Messagemedia SMS server
via port 80 (or port 443 for encrypted SSL communications), either directly or via a
proxy server.
The first step is to contact a list of active SMS servers in sequence until there is a
positive response from one of them. Having found an active server, the API will then
send through a username and password for authentication, then issue a command
and extract information from the server's response.
Installation
If you are running PHP 4.2 or above, you should install the file SmsInterface.inc. If
you are running an older version, install the file SmsInterface-40.inc instead, but
rename it to SmsInterface.inc
Since the file is distributed as source code, it can be installed anywhere that your
PHP interpreter has read access. For convenience, we suggest that you install it in
the same directory as the PHP application that will be using it.
If your system does not have a PHP interpreter installed, visit http://www.php.net to
download the latest version and follow the installation instructions provided.
The following section will explain how to use the API through the use of examples. A
more detailed description of the individual API components can be found in the
Reference section.
Sending Messages
<HTML>
<HEAD><TITLE> SMS sending results </TITLE></HEAD>
<BODY>
<?php
require ("SmsInterface.inc");
This is an example of a PHP script that would be called from a web server. The script
will connect to the Messagemedia server and send out a single SMS message to a
specified phone number. The source code for this script can be found in the
examples directory of this distribution under the name sendsms.php. It could be
called from the following HTML script (which is also included in the examples
directory under the name sendsmsform.html).
<HTML>
<HEAD><TITLE> Send SMS </TITLE></HEAD>
<BODY>
<FORM ACTION="sendsms.php" METHOD="POST">
Username: <INPUT TYPE="text" NAME="username" SIZE="10"> <BR>
Password: <INPUT TYPE="password" NAME="password" SIZE="10"> <BR>
Phone number: <INPUT TYPE="text" NAME="phone" SIZE="13"> <BR>
Message:
<INPUT TYPE="text" NAME="message" SIZE="80" MAXLENGTH="160">
<BR>
In the PHP code, the line require ("SmsInterface.inc"); includes the API and is
needed by every application that wishes to use it.
The API uses an object-oriented design, whose main class is SmsInterface. The
constructor for this class takes two optional parameters - autoSplit which specifies
whether message splitting will take place (a boolean value, which is optional and
defaults to false), and concatenate which specifies that message concatenation will
take place (also boolean, defaulting to false). These parameters are used to decide
how to treat messages greater than 160 characters in length. If both are false, all
such messages will be truncated to 160 characters. However, if autoSplit is true,
long messages will be split into separate messages smaller than 160 characters and
delivered at 30 second intervals, while if concatenate is true, long messages will be
concatenated into a single long message using an extension to the GSM protocol.
Note that this extension may not work on older phones.
The $si->addMessage() function adds a message to an internal list. You can add as
many messages as you like, and they will be sent in a batch when $si-
>sendMessages() is called. However, it is recommended that you send batches of no
more than 200 messages, to minimize both the time it takes to complete the task
and the chance that a network or server error will cause the batch to fail. Note that if
there is an error, no messages from the batch will be sent, so it is safe to re-send
the entire batch.
In the example above, the addMessage function is taking phone number and
message parameters, both strings. You can also specify extra parameters, setting a
message ID, scheduled delay, validity period, and whether a delivery report is
required. For details, see the Reference section of this document.
If the connection is successful, we then send the messages with the $si-
>sendMessages() command. Note that this command will close the connection
before returning, so you must re-connect if you wish to issue any other commands to
the server. If this call fails, the server may provide a reason, which we can access
via the $si->getResponseMessage() function.
Reading Messages
<HTML>
<HEAD><TITLE> Messages retrieved from server </TITLE></HEAD>
echo "</TABLE>\n";
}
?>
</BODY>
</HTML>
The above script will connect to the Messagemedia server, using a username and
password specified in the calling form, and display all the previously-unread replies
in a table. The source code for this script can be found in the examples directory
under the name readsms.php.
After connecting to the server with the $si->connect() call, the $si->checkReplies()
call will return an array of SmsReply objects and close the connection to the server.
Note that if there is a problem, such as an incorrect password, this function will
return NULL, while a successful connection with no replies will return an empty array.
You should test for a NULL return by comparing with the === operator. Do not use
the == operator, since it will not distinguish between NULL and an empty array.
The SmsReply class contains a messageID field, which corresponds to the message
ID of the message that it is replying to. The phoneNumber and message fields are
self-evident. The status field is used to indicate delivery reports, which may be
received if you previously sent a message that requested one.. The SmsReply class
also has another field not shown, $sr->when, which indicates how many seconds ago
the reply was received. See the Reference section for further details
Delivery Reports
When sending an SMS, it is possible to request a delivery report from the network.
This is done by setting the 6th parameter of the $si->addMessage()to true. However,
it should be noted that messages requesting delivery reports cost more to send than
regular messages. Please contact Messagemedia Pty Ltd for detailed pricing
information.
When the SMS is successfully sent (but not yet received), a delivery report will be
sent to your account by the telecommunications carrier. This report can be retrieved
via the checkReplies function, where it will appear in the form of an SmsReply where
the field $sr->status is equal to MessageStatus::pending().
<HTML>
<HEAD><TITLE> Change password result </TITLE></HEAD>
<BODY>
<?php
require ("SmsInterface.inc");
The above script will connect to the Messagemedia server and change the password
for the user's account. The source code for this script can be found in the examples
directory under the name password.php.
The $si->changePassword() function takes the new password as its argument, which
it passes to the server before closing the connection.
<HTML>
<HEAD><TITLE> Credits remaining </TITLE></HEAD>
<BODY>
<?php
require ("SmsInterface.inc");
if ($cr == -2) {
echo "Unable to read credits for " . $_POST["username"];
echo " from the SMS server.\n";
if ($si->getResponseMessage () !== NULL)
echo "<BR>Reason: " . $si->getResponseMessage() . "\n";
} elseif ($cr == -1) {
echo "The account for " . $_POST["username"];
echo " is not a pre-paid account.\n";
} else {
echo "The account for " . $_POST["username"];
The above script will connect to the Messagemedia server and return the number of
credits remaining for the user's account. The source code for this script can be found
in the examples directory under the name credits.php.
If you are using the secure SSL option and HTTPS communications go via a proxy
server, you will need to call the setHttpsProxy() function. Assuming this is done via
port 443, and the proxy server authenticates with username “john” and password
“xx”, the code might look like this.
class SmsInterface
SmsInterface (
$autoSplit, // Boolean, optional, default = false
$concatenate // Boolean, optional, default = false
);
The SmsInterface constructor takes two parameters, which specify how long
messages will be sent.
If both the autoSplit and concatenate parameters are false, all long messages will be
truncated to 160 characters. However, if autoSplit is true, long messages will be split
into separate messages smaller than 160 characters and delivered at 30 second
intervals, while if concatenate is true, long messages will be concatenated into a
single long message using an extension to the GSM protocol. Note that this
extension may not work on older phones.
$si->connect (
$username, // String
$password, // String
$useMessageID, // Boolean
$secure // Boolean
);
$si->getResponseCode ();
This function returns the integer response code received from calls to
changePassword, getCreditsRemaining, sendMessages, and checkReplies. A value of
-1 indicates that no response code has been received from the server.
Values in the 100-199 range indicate success. 400-499 indicate temporary errors
(e.g. network down), while 500-599 indicate permanent errors (e.g. wrong
password). Some common values are:
$si->getResponseMessage ();
This function returns the response message received from calls to changePassword,
getCreditsRemaining, sendMessages, and checkReplies. A value of NULL indicates
that no response code has been received from the server. The messages correspond
to the codes returned by $si->getResponseCode().
$si->addMessage (
$phone, // String
$messageText, // String
$messageID, // Integer, optional, default = 0
$delay, // Integer, optional, default = 0
$validityPeriod, // Integer, optional, default = 169
$deliveryReport // Boolean, optional, default = false
);
The $si->addMessage() function adds a message to an internal list. You can add as
many messages as you like, and they will be sent in a batch when $si-
>sendMessages() is called. However, it is recommended that you send batches of no
more than 200 messages, to minimize both the time it takes to complete the task
and the chance that a network or server error will cause the batch to fail.
The $phone parameter is a string containing the phone number. This can be a local-
format Australian mobile number (e.g. 0410123456), an international-format
Australian mobile number (e.g. 61410123456), or a full international number (e.g.
+447746123456). Any spaces and non-numeric characters will be stripped out
before processing, so it is safe to separate the digits by spaces.
The $messageID is an integer that will be passed back if there are any replies to this
message. Careful selection of this value can allow messages to be correlated with
replies.
The $delay parameter is an integer that specifies how many seconds in the future
the message should be delivered.
The $validityPeriod parameter specifies how long the SMS network should keep
trying to deliver the message if the recipient's phone is not responding. Suitable
values are specified in the ValidityPeriod class. The default value is 3 days.
If the $deliveryReport parameter is set true, the SMS network will send a delivery
report when the message is received by the network, and again when the message is
either successfully delivered, or discarded after the validity period expires.
$si->sendMessages ();
This function returns a boolean value, true on success, false on failure. In the case of
failure, the reason may be provided by the getResponseCode and
getResponseMessage functions.
$si->changePassword (
$newPassword // String
);
This command changes the account's password to the specified value, and closes the
connection to the server before returning.
The function returns a boolean value, true on success, false on failure. In the case of
failure, the reason may be provided by the getResponseCode and
getResponseMessage functions.
$si->getCreditsRemaining ();
$si->checkReplies (
$autoConfirm // Boolean, optional, default = true
);
If the $autoConfirm parameter is true, after downloading the array of replies the
function will automatically re-connect to the server and confirm that they been
successfully received. If the parameter is false, however, the replies will not be
marked as read on the server until there is an explicit call to $si-
>confirmRepliesReceived() (see below).
$si->setHttpProxy (
$proxyName, // String, may be full name or IP address
$proxyPort, // Integer
$proxyUsername, // String, optional, default = NULL
$proxyPassword // String, optional, default = NULL
);
$si->setHttpsProxy (
$proxyName, // String, may be full name or IP address
$proxyPort, // Integer
$proxyUsername, // String, optional, default = NULL
$proxyPassword // String, optional, default = NULL
);
class SmsReply
The SmsReply class contains a number of fields whose values contain reply
information. Given an SmsReply object $sr, the fields are as follows.
$sr->phoneNumber
This is a string value, containing the phone number of the sender of the message.
Note that the number will be in international format including the + symbol, e.g.
+61410123456.
$sr->message
$sr->messageID
This is an integer, whose value matches the message ID of the message that is being
replied to.
$sr->when
This is an integer value, specifying how many seconds ago the reply was received by
the Messagemedia server.
$sr->status
This integer value indicates whether the reply is a message or some sort of delivery
receipt. It can take the following values.
class ValidityPeriod
This class returns the following integer values, which can be used as the fifth
parameter in the SmsInterface addMessage() function.
• ValidityPeriod::minimum () // 5 minutes
• ValidityPeriod::oneHour ()
• ValidityPeriod::sixHours ()
• ValidityPeriod::oneDay ()
• ValidityPeriod::twoDays ()
• ValidityPeriod::threeDays ()
• ValidityPeriod::oneWeek ()
• ValidityPeriod::maximum () // 63 weeks