Wednesday, January 12, 2011

How to send and receive sms using php code?

The idea we use is to have an HTML form, that allows the website visitor to enter a phone number and the message text. This HTML form can be submitted by clicking on the "SEND" button. Once submitted the form variables will be processed by a PHP script. This PHP script will post the SMS message to the SMS Gateway (Figure 1). As you can see on Figure 1. The SMS Gateway and your webserver (your webserver is hosting your HTML Form and your PHP script) can be on two different computers.

Step 1: Setting up the system and Install the Ozeki NG SMS Gateway to your computer to verify, that you can send SMS messages from the gateway manually. After your SMS gateway is functioning you can create the HTML Form and the PHP script.
Step 2 : Create the HTML Form for sms sending and save the sendsms.html into www directory of webserver . Html Form can be used to get the recipient telephone number and the message text from the website visitors.
Source code :
C:/xampp/htdocs/sms/sendsms.html


My SMS form

Recipient
Message



Save this file.

Please note that the form action tag points to the sendsms.php file. This means that the data entered by the website visitor will be posted to the sendsms.php file.

Step 3: Prepare php sms script.
form target in this HTML form is the sendsms.php. The sendsms.php script will get the recipient and the message fields from the form and will build a URL to send the SMS message. This URL will be used to pass the SMS message to the SMS gateway. he URL is invoked in the PHP script using the file command. To get this script working you should configure the $ozeki_url variable to point to the SMS gateway. If you have installed the SMS Gateway to the same computer where your webserver is you can leave this untouched, otherwise you should change the IP address in the URL.

Source code :

$urltouse

"; }

//Open the URL to send the message
$response = httpRequest($urltouse);
if ($debug) {
echo "Response:
".
           str_replace(array("<",">"),array("<",">"),$response).
           "

"; }

return($response);
}

########################################################
# GET data from sendsms.html
########################################################

$phonenum = $_POST['recipient'];
$message = $_POST['message'];
$debug = true;

ozekiSend($phonenum,$message,$debug);

?>

Step 4 : Finish

Once you have both: sendsms.html and sendsms.php installed, you can submit an SMS to the gateway. On a successful send the SMS gateway will return the message reference number. This number can be used to track the message inside the SMS Gateway.


NOTE: In order to be able to use this PHP script, you need to have the Ozeki NG SMS Gateway software installed to your computer. After Ozeki NG SMS Gateway is installed and configured, change the value of the $ozeki_url variable in the above PHP script to reflect the IP address of your computer. In other words, if you install Ozeki NG SMS Gateway to the computer, that has an IP address of 101.102.103.104, you should change the http://127.0.0.1:9501/api? value to http://101.102.103.104:9501/api?. Note that the ip address 127.0.0.1 has been changed in the $ozeki_url variable.

1 comment:

  1. Thanks, Such a nice article. HTTP API is most popular API. It is allows you to integrate SMS services into your own System/application for better functionality required by you and send Bulk SMS API PHP code messages.

    ReplyDelete