Note: For additional APIs for sending and receiving SMS and MMS messages, please see our Developer APIs for SMS + MMS page.
Back in 2003, we posted an example PHP script for sending SMS via NowSMS on our discussion board at http://www.nowsms.com/discus/messages/1/867.html.
While this script has worked well over the years, it has a serious limitation in that it does not support SSL/TLS. Version 2 of this script adds support for SSL/TLS.
If you are migrating from the version 1 script, SSL/TLS requires a slight change in parameters. The first two parameters for the old version were host name or IP, followed by the port number. In the new version, instead these are combined into a single parameter, which is the URL of the NowSMS server (e.g., http://127.0.0.1:8800 or https://sample.smshosts.com/).
The Version 2 script can be downloaded at the following link: http://www.nowsms.com/download/sendsms-php.txt.
The SendSMS function is the important part of the example. This is the function that needs to be included in your PHP script. You call this function, specifying the base URL of the NowSMS server, along with a username and password for an “SMS Users” account on the NowSMS server, plus the recipient phone number and text of the SMS message.
The SendSMS function uses these parameters to build an HTTP POST for connecting to the NowSMS server.
Additional optional parameters are supported. For additional information on NowSMS URL parameters, see http://www.nowsms.com/doc/submitting-sms-messages/url-parameters.
Following the SendSMS function, we show two examples of how this function might be called from within a PHP script. The first example sends an SMS text message with only the required parameters. The second example includes an extra optional parameter to illustrate how these parameters are encoded.
// This code provides an example of how you would call the SendSMS function from within
// a PHP script to send a message.
SendSMS('https://sample.smshosts.com/', 'username', 'password', '+44999999999', 'Test Message');
// This example adds an additional URL parameter, ReceiptRequested=Yes
SendSMS('https://sample.smshosts.com/', 'username', 'password', '+44999999999', 'Test Message with delivery report', 'ReceiptRequested', 'Yes');
The script detailed below can be downloaded at the following link: http://www.nowsms.com/download/sendsms-php.txt.
"$phoneNoRecip", 'Text'=>"$msgText");
if (($n1 != NULL) && ($v1 != NULL)) $postfields[$n1] = $v1;
if (($n2 != NULL) && ($v2 != NULL)) $postfields[$n2] = $v2;
if (($n3 != NULL) && ($v3 != NULL)) $postfields[$n3] = $v3;
if (($n4 != NULL) && ($v4 != NULL)) $postfields[$n4] = $v4;
if (($n5 != NULL) && ($v5 != NULL)) $postfields[$n5] = $v5;
if (($n6 != NULL) && ($v6 != NULL)) $postfields[$n6] = $v6;
if (($n7 != NULL) && ($v7 != NULL)) $postfields[$n7] = $v7;
if (($n8 != NULL) && ($v8 != NULL)) $postfields[$n8] = $v8;
if (($n9 != NULL) && ($v9 != NULL)) $postfields[$n9] = $v9;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $hostUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
// TODO: This script does not currently validate SSL Certificates
// curl_setopt($ch, CURLOPT_VERBOSE, true);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
// curl_setopt($ch, CURLOPT_CAINFO, 'cacert.pem');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // change to 1 to verify cert
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($ch);
return $result;
}
// This code provides an example of how you would call the SendSMS function from within
// a PHP script to send a message.
// The response from the NowSMS server is echoed back from the script.
$x = SendSMS('https://sample.smshosts.com/', 'username', 'password', '+44999999999', 'Test Message');
echo $x;
// This example adds an additional URL parameter, ReceiptRequested=Yes
$x = SendSMS('https://sample.smshosts.com/', 'username', 'password', '+44999999999', 'Test Message with delivery report', 'ReceiptRequested', 'Yes');
echo $x;
?>