Send SMS from PHP Script

Posted by on Jul 14, 2017 in Support Blog

Topic Keywords: , ,

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 https://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: https://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 https://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: https://nowsms.com/download/sendsms-php.txt.

 

<?php

 

function SendSMS ($hostUrl, $username, $password, $phoneNoRecip, $msgText,

                  $n1 = NULL, $v1 = NULL, $n2 = NULL, $v2 = NULL, $n3 = NULL, $v3 = NULL, 

                  $n4 = NULL, $v4 = NULL, $n5 = NULL, $v5 = NULL, $n6 = NULL, $v6 = NULL, 

                  $n7 = NULL, $v7 = NULL, $n8 = NULL, $v8 = NULL, $n9 = NULL, $v9 = NULL  ) { 

 

// Parameters:

//  $hostUrl – URL of the NowSMS server (e.g., http://127.0.0.1:8800 or

//             https://sample.smshosts.com/

//  $username – “SMS Users” account on the NowSMS server

//  $password – Password defined for the “SMS Users” account on the NowSMS Server

//  $phoneNoRecip – One or more phone numbers (comma delimited) to receive the message

//  $msgText – Text of the message

//  $n1-$n9 / $v1-$v9 - Additional optional URL parameters, encoded as name/value pairs

//                      Example: charset=iso-8859-1 encoded as 'charset', 'iso-8859-1'

 

   $postfields = array('Phone'=>"$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_BASIC);

   curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

   curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); 

   $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;

 

?>

For comments and further discussion, please click here to visit the NowSMS Technical Forums (Discussion Board)...

7 Responses to “Send SMS from PHP Script”

  1. I try NowSMS, seems to good for my purpose building 2 way sms server.

    But i wonder how can i handle voice call to automatically dropped when GSM number called by other party (i use dedicated PCMCIA modem, sierra aircard 875).

    I can’t use manual AT command to do that since port already used by NowSMS.

    Any idea?
    Or maybe a feature request to automatically reject incoming voice call on NowSms server.

  2. Bryce Norwood says:

    Hi,

    Apologies for the delay in response. Our comment notification setup for blogger was not working.

    I believe you also posted your query over on our discussion forum, where we responded to it at:

    https://nowsms.com/discus/messages/1/24457.html

    -bn

  3. please help me,
    I want to send sms to multiple numbers. Php script?

    Tanks

  4. An updated version of this script which supports additional NowSMS URL parameters (such as for specifying an explicit outbound SMSC route or a sender address) can be found at https://nowsms.com/send-sms-from-php-script-updated.

  5. HI there,
    SO ive got the form working but i want to add default message at the end of the message.
    so the user types in the message and when the SMS is sent it should also have another text after the users typed message. how do i do that?

    • Either use JavaScript in your form to append text on submit …

      Or hard code the added text within the PHP script. An example of adding it to the PHP script follows.

      Change this:

      fwrite($fp, “GET /?Phone=” . rawurlencode($phoneNoRecip) . “&Text=” . rawurlencode($msgText) . ” HTTP/1.0\n”);

      to this:

      $addText = ” (Powered by Me)”;

      fwrite($fp, “GET /?Phone=” . rawurlencode($phoneNoRecip) . “&Text=” . rawurlencode($msgText) . rawurlencode($addText) . ” HTTP/1.0\n”);