I have been using Drupal for about a month now and boy o boy I wonder how I never heard about it. I have built a site for ahome delivery service in Nigeria www.granmakitchenng.com
I am trying to integrate the order form to send sms messages. I successfuly created a rule to send sms via an email to sms service (www.etextmail.com. This works fine to an extent though it keeps truncating my sms text. it does not do this when i send the same content directly via email. so it seems my drupal form is somehow doing it even though i don't see the logic since my email looks complete when i check it out.
I got the API from the gateway service but I would need to integrate it to Drupal. Rather than do a shuddy job, I thought it would be nice to extend the SMS Framework module.
I need pointers on how to approach this. I already went through the onthisdate module tutorial, so i am getting there somehow. It looks doable and I would love to work on this with help from the community. Drupal sure rocks. Now I know why it has been tough building a great site in a short time. We try alone
Can anyone assist. There are 3 files.
SendSms.html - HTML file for sending SMS via the web using PHP
SendSmsHttp.php - PHP source code for sending SMS
PostRequest.php - PHP source code for posting HTTP requests to the API server
//------------------------------PostRequest.php
/*=============================================================================================
** The function to post a HTTP Request to the provided url passing the $_data array to the API
===============================================================================================*/
function PostRequest($url, $_data) {
// convert variables array to string:
$data = array();
while(list($n,$v) = each($_data)){
$data[] = "$n=$v";
}
$data = implode('&', $data);
// format --> test1=a&test2=b etc.
// parse the given URL
$url = parse_url($url);
if ($url['scheme'] != 'http') {
die('Only HTTP request are supported !');
}
// extract host and path:
$host = $url['host'];
$path = $url['path'];
// open a socket connection on port 80
$fp = fsockopen($host, 80);
// send the request headers:
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: ". strlen($data) ."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data);
$result = '';
while(!feof($fp)) {
// receive the results of the request
$result .= fgets($fp, 128);
}
// close the socket connection:
fclose($fp);
// split the result header from the content
$result = explode("\r\n\r\n", $result, 2);
$header = isset($result[0]) ? $result[0] : '';
$content = isset($result[1]) ? $result[1] : '';
// return as array:
return array($header, $content);
}
//---------SendSmsHttp
/*Requrire the PostRequest function for posting the API request to the server */
require ("PostRequest.php");
$username = $_POST['txtUserName'] ;
$password = $_POST['txtPassword'] ;
$senderId = $_POST['txtSenderId'];
$destination = $_POST['txtDestination'];
$longSms = $_POST['ckcLongSms'];
$message = $_POST['txtMessage'];
/* ==================================================
SUBMIT A REQUEST TO SEND THE SMS TO VIA THE HTTP API
=====================================================*/
// submit these variables to the server
$data = array( 'UN' => $username,
'p' => $password,
'SA' => $senderId,
'DA' => $destination,
'L' => $longSms,
'M' => $message);
// send a request to the API url
list($header, $content) = PostRequest("http://74.126.82.44/smsapi/Send.aspx?", $data);
// display the result of the request
//echo $content . '<br>';
$tok = strtok($content, " "); //Split the $content result into workds
if($tok == "OK") //Success
{
$tok = strtok(" ");
echo "Sms sent succesfully using " .$tok . " SMS credits(s).";
}
else{
//Diaply the full error message
echo "The following error occured: <br> ". $content . "<br>";
}
/* =========================================================================
SUBMIT ANOTHER REQUEST TO GET THE NUMBER OF SMS BALANCE IN THE ACCOUNT
============================================================================*/
//submit these variables to the server
$data2 = array( 'UN' => $username,
'p' => $password);
list($header2, $content2) = PostRequest("http://74.126.82.44/smsapi/GetCreditBalance.aspx?", $data);
// display the result of the request
//echo $content . '<br>';
$tok2 = strtok($content2, " "); //Split the $content result into workds
if($tok2 == "OK") //Success
{
$tok2 = strtok(" ");
echo "<br>Account SMS Credit Balance: " . $tok2 . "<br>" ;
}
else{
//Diaply the full error message
echo "The following error occured: <br> ". $content2 . "<br>";
}
//---------------SendSms.html
// anchored in php to let the code display
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Sample Contact Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body >
<form name="form1" method="post" action="sendsmshttp.php">
<table style="width: 765px" border="1" cellpadding="5" cellspacing="0" align="center">
<tr>
<td colspan="2"> <h1 style="text-align: center"> Send SMS Message via API</h1></td>
</tr>
<tr>
<td style="width: 225px"> SMS Account User Name:</td>
<td> <input name="txtUserName" type="text" id="txtUserName" /></td>
</tr>
<tr>
<td style="width: 225px"> SMS Account Password:</td>
<td> <input name="txtPassword" type="text" id="txtPassword" /></td>
</tr>
<tr>
<td style="width: 225px"> </td>
<td> </td>
</tr>
<tr>
<td style="width: 225px"> Sender ID:</td>
<td> <input name="txtSenderId" type="text" maxlength="11" id="txtSenderId" /></td>
</tr>
<tr>
<td style="width: 225px" valign="top"> Destination Number(s):</td>
<td valign="top"> <input name="txtDestination" type="text" id="txtDestination" style="width:361px;" />
<br /> <em><span style="font-size: 0.8em">Comma separated numbers in international
format. Ex. 2348031234567, 2348024567891</span></em></td>
</tr>
<tr>
<td valign="top"> Message:</td>
<td valign="top"> <textarea name="txtMessage" rows="2" cols="20" id="txtMessage" style="height:123px;width:381px;"></textarea></td>
</tr>
<tr>
<td style="width: 225px"></td>
<td><input type="checkbox" name="ckcLongSms" value="1">
Long SMS. Combine more than 160 characters to be displayed as 1 long SMS
at the receiver's cell phone. Upto 459 characters can be delivered as
1 long SMS. Charged at 1.2 credits per message. </td>
</tr>
<tr>
<td style="width: 225px"> </td>
<td> </td>
</tr>
<tr>
<td style="width: 225px"> </td>
<td> <span id="lblResponse">
<input type="submit" name="btnSend" value="Send SMS Message" id="btnSend" />
</span></td>
</tr>
</table>
</form>
</body>
</html>
// anchored in php to let the code display
Comments
Pointer for making a new gateway module
Hi tiwiex, congratulations on the Drupal and SMS Framework discovery :-)
You are 100% correct that integrating the SMS Framework with the API is the best way to work with SMS messages - email will the unpredictable and difficult to debug. Building a new gateway module for the SMS Framework is the way to do this, and requires a basic understanding of PHP code. How confident are you with PHP?
The example code all looks good, so you would just need to place the PHP code into a new gateway module. See Writing a Gateway Module for help. You can use an existing gateway module as a template - I would recommend using the txtlocal module as an example because the code is simpler than the clickatell module.
Good luck, and let us know if you need help with any specific part.
~ap
Thanks
I appreciate your response. I will look through your pointers as suggested. I am sure I will come up with something. I have worked with PHP in the past but not using such neat codes. I am sure I will get a hang of it. Thanks again.
Hi tiwiex, I hope that you've
Hi tiwiex,
I hope that you've managed out writing your sms gateway integration.
Actually I am doing the same thing with a local Tunisian SMS gateway. The goal is to permit to users to grow their userpoints by sending SMS's. And then they can use their points to do stuff on the site.
Actually I have almost written the gateway module. It is a simple HTTP GET based gateway.
The problem is that in the docs it is mentioned that to receive SMS we must implement some functions like this :
function my_gateway_incoming_callback() {
// Any necessary processing to extract the number and message
sms_incoming($number, $message, $options);
}
that would handle the incoming SMS and pass it to the sms framework.
My question is by wich url do I have to pass the SMS params so that this function is called. In other words, I need to provide an url to my SMS provider so he can send me on it HTTP GET requests. What will be the url that I have to provide him ?
Thanks in advance
Another Gateway
Can anyone do the same for another HTTP based SMS Gateway? Here is the Api Documentation (in german, but easy to understand) http://www.lox24.eu/docs/api.pdf
Help me with this API
pls how can i send a birthday reminder with this API... i wont need the form part..all i need is a script that will go into my database and select all the date of birth that is exactly the same as that date of the day..then if it exist select the Phone number and send a message.....pls help me i dont know how to integrate it into this API..