How to Send SMS With PHP Code

How to Send SMS With PHP Code thumbnail
Use PHP to send text messages from your website.

SMS (Short Message Service) is the technology behind the popular text message. SMS is usually used by cellular phones. Some website owners have been tapping into the power and popularity of SMS to allow their users to send SMS messages (i.e. text messages) from their websites. You can offer the same benefit to your website visitors. Use the PHP scripting language to add SMS functionality to your website. You will use PHP to prepare the request to send a SMS message and then use the CURL library installed on the server to send it.

Instructions

    • 1

      Create an account with one of the SMS gateway providers, such as Clickatell, Ozeki NG and TM4B SMS Gateway (see Resources). The gateway provider will be in charge of sending your requests for SMS messages. As with sending text messages from cell phones, you will need a service provider in order to send SMS messages from your website. The gateway provider will provide you with a paid solution for sending SMS messages from your website.

    • 2

      Start a new blank file in a web-authoring application, such as Dreamweaver, or a plain text-editing application like Notepad. Most web-authoring application uses a WSIWYG (What You See is What You Get) environment, in which you just add the visual elements to the document and the program will automatically generate the code. You want to type in your own code in this project, so switch to "Code" or "HTML" view of the program. Also, don't use a word processor as a text-editing program. Word processors, like Microsoft Word, add extra formatting code that will interfere with the PHP code.

    • 3

      Create the web form that will work as the user interface for entering in the information for sending the text message. Visitors to your website will type in the information for the text message. You will then collect this information and send it in the request to the SMS gateway.

      "<html>

      <head>

      <body>

      <form action="<?php print $PHP_SELF?>" enctype="multipart/form-data" method="post">

      Sender:<br /> <input type="text" name="sender" value="" /><br />

      Receiver:<br /> <input type="text" name="receiver" value="" /><br />

      Message: <p><textarea name="message" rows="20 cols="80">

      Click in this box and type in your text message.

    • 4

      Type in the PHP code to prepare the request to the messaging service's gateway. You have to send the gateway an HTTP request that passes along the details of the SMS message. The request should look like this:

      "<?php

      \\Takes the information from the form and save it to variables

      $sender = $_POST['sender'];

      $receiver = $_POST['receiver'];

      $message = $_POST['message'];

      \\Prepare the request

      $request = "";

      $param["username"] = "username"; //the account username provided by gateway provider

      $param["password"] = "password"; //the account password provided by gateway provider

      $param["msg"] = $message; //the text message that will be sent

      $param["to"] = $receiver; //the recipient of the text message

      $param["from"] = $receiver; //the message sender

      $param["route"] = "frst"; //this means we are sending the message first class

      $param["sim"] = "yes";

      foreach($param as $key=>$val){

      $request.= $key."=".urlencode($val);

      $request.= "&";

      }

      $request = substr($request, 0, strlen($request)-1);

      ?>"

      Change the param values above to meet your specific details.

    • 5

      Type in the code to actually send the SMS message. PHP has a CURL library included that handles these types of things.

      "<?php

      $url = "{add the url to the gateway API}"; //the gateway's interface provided by the gateway provider

      $ch = curl_init;

      curl_setopt($ch, CURLOPT_URL, $url);

      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

      curl_setopt($ch, CURLOPT_POST, 1);

      curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

      $response = curl_exec($ch);

      curl_close($ch);

      ?>

      </body>

      </head>

      </html>"

      Do not change any of the code above other than removing the quotation marks from the first and last line in the code and adding the URL to the SMS gateway provider's API interface. Check with the provider for this information. Save the document with the PHP file extension and upload it to your web server.

Related Searches:

References

Resources

  • Photo Credit New message on 3d rendered cell phone image by Franc Podgor...¡ek from Fotolia.com

Comments

You May Also Like

  • How to Send a Free SMS Using PHP

    Short message service (SMS) messages, also known as text messages, have become the preferred way of communication for many people. They offer...

  • How to Send an SMS with a PHP Script

    With nearly everyone carrying a cellular phone, Short Message Service (SMS) is a powerful way to reach people wherever they are. Your...

  • How to Send SMS Through PHP

    SMS, or short message service, is a communication service available to mobile phone subscribers. It allows users to send snippets of text...

  • How to Build an SMS Website

    People who like to chat online enjoy SMS or short message service websites. Users normally send this type of communication in the...

  • How to Send a SMS Using PHP Script

    SMS stands for "short messaging service" and it is the basis for text messaging on cell phones. Traditionally, these messages could be...

  • How to Send Web SMS

    SMS, better known as text messages, are small messages typically sent via wireless phone to other phones that have the SMS feature...

  • How to Send an SMS Code

    A Short Messaging Service (SMS) is a service that allows you to send a text message from one SMS capable phone to...

  • How to Send SMS Net Codes

    Short Message Service (SMS) allows the user to send short messages that are up to 160 characters long over a wireless device....

  • How to Send SMS Messages From a Mac

    SMS stand for "short message service". It is used to send messages up to 160 characters long to mobile devices, email or...

  • How to Send SMS to MTN Short Codes

    Based in South Africa, the mobile communications company MTN makes SMS messaging available to all of its customers. MTN short codes consist...

  • How to Send SMS Through a Gateway

    An SMS gateway allows devices other than cell phones to send messages through a mobile network. This way, a computer can send...

  • How to Send Free SMS Using Gmail

    There are many reasons why people choose specific email providers, including Gmail. If you are a Gmail user, one of the benefits...

  • How to Send SMS Through Web Applications

    You can send Short Message Service (SMS) to mobile phone users in different countries of the world. There are a lot of...

  • How to Send an SMS to Short Code

    A short code is typically a five-digit number that is used for text messaging, often by a company for marketing purposes. Users...

  • How to Send an SMS Worldwide

    The process of sending international SMS in countries outside the United States depends on the more specific requirements of the telecom company...

  • How to Send an SMS Telecom From Hotmail

    Sending an email from your Hotmail to a cell phone is just as easy as sending an email to another Hotmail user....

  • How to Create Simple Form With Email Notification

    A Web form with Email notification has two parts to it--the HTML portion and the PHP code. The HTML portion is used...

  • How to Send a Free SMS Through the Web

    SMS messages, also called text messages, are a way to communicate with a friend or family member quickly. However, that can become...

  • How to Send SMS With a Program

    SMS messages, or "text" messages, are a popular form of communication generally sent and received by cell phones. The standard SMS message...

Related Ads

Featured