Jump to content

How to Send SMS With PHP Code


dogsinfo

Recommended Posts

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.