How to Send SMS With PHP Code

Written by tiesha whatley
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Send SMS With PHP Code
Use PHP to send text messages from your website. (New message on 3d rendered cell phone image by Franc Podgor...¡ek from

SMS (Short Message Service) is the technology behind the popular text message. SMS is usually used by mobile 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.

Skill level:

Other People Are Reading


  1. 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. 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. 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.




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

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

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

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

    Click in this box and type in your text message.

  4. 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:


    \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. 5

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


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






    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.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.