Jump to content

SMS using PHP and WSDL


SalientAnimal

Recommended Posts

Hi All,

 

I'm not 100% sure if this is the place to be posting for help with this. I have managed to get some source code for a SMS PHP page. However I am having some problems getting it to work, mainly because I'm not 1005 sure how to configure it, and if I need to make any toher changes to the code in order to incorporate my SMS gateway.

 

The error message I am getting is: "SMS Sending Failed Message:Network error unable to send the message", this mesage is however coded into the code.

 

Below is the code of the 4 different pages:

 

HTML Page where message is created

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
   <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
   <title>SMS Sending Service</title>
   <style type="text/css">
    body { font-family:Verdana,Arial,sans-serif; font-size:100%; }
    td { vertical-align:top; }
    input[type=submit] { padding:5px 10px; }
   </style>
<script type="text/javascript" src="send.js"></script>
</head>
<body>
   <h1>SMS Sending Service</h1>
   <form action="send.php" method="post">
    <table border="0" cellpadding="5" style="background:#F7F7F7;border:1px solid #CCC;border-radius:5px;-moz-border-radius:5px;">
	    <tr>
		    <td><label for="method">Tipo di SMS:</label></td>
		    <td>
			    <select name="method" id="method">
				    <option value="classic">Classic</option>
				    <option value="report">Classic con notifica</option>
				    <option value="basic">Basic</option>
			    </select>
		    </td>
	    </tr>
	    <tr>
		    <td><label for="recipients">Your Valentine's Number: </label></td>
		    <td>
			    <span id="recipients">
				    <div><input type="text" name="recipients[]" value="27" /></div>
			    </span>
			    <a href="javascript:;" onclick="addRecipient();"><small>Add Another Valentine</small></a>
		    </td>
	    </tr>
	    <tr>
		    <td><label for="text">Message: </label></td>
		    <td><textarea name="text" id="text" cols="30" rows="10"></textarea></td>
	    </tr>
  <tr>
   <td></td>
   <td>
 <small>
 Characters Remaining: <span id="leftChars" style="color:#F00;">0</span> /
 <b id="messageMaxLength">765</b>
 (<span id="numberOfSMS" style="color:#39b54a;">1</span>)
 </small>
   </td>
  </tr>
	    <tr>
		    <td><label for="sender_number">Sender's Number: </label></td>
		    <td><input type="text" name="sender_number" id="sender_number" /> <small>Eg 27741123456</small></td>
	    </tr>
	    <tr>
		    <td><label for="sender_string">Alphanumeric Sender: </label></td>
		    <td><input type="text" name="sender_string" id="sender_string" maxlength="11" /> <small>Max 11 Characters</small></td>
	    </tr>
	    <tr>
		    <td><label for="charset">Charset</label></td>
		    <td>
			    <select name="charset" id="charset">
				    <option value="ISO-8859-1">ISO-8859-1</option>
				    <option value="UTF-8">UTF-8</option>
			    </select>
		    </td>
	    </tr>
	    <tr>
		    <td colspan="2" align="right">
			    <input type="submit" value="Send Message" name="submit" />
		    </td>
	    </tr>
    </table>
   </form>
</body>
</html>

 

PHP Send page

<?php
// IMPORTANT:
// ENTER YOUR CREDENTIALS HERE VM
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
define('VM_WSDL', 'https://myhostserver/WebsiteData/WebsiteDataEndpoint?wsdl');
define('VM_USERNAME','MyGateWayUsername');
define('VM_PASSWORD','MyGateWayPassword');
require('validate.php');
// Collect post data from form
$method = $_POST['method'];
$recipients = $_POST['recipients'];
$text = $_POST['text'];
$sender_number = $_POST['sender_number'];
$sender_string = $_POST['sender_string'];
$charset = $_POST['charset'];
$result = VMGatewaySendSMS(VM_USERNAME, VM_PASSWORD, $recipients, $text, $method, $sender_number, $sender_string, $charset);
if($result['status']=='success') {
 echo '<b style="color:#8dc63f;">SMS Sent Successfully</b><br/>';
 echo '<b>SMS rimanenti:</b>'.$result['remaining_sms'];
}
if($result['status']=='failed') {
 echo '<b style="color:#ed1c24;">SMS Sending Failed</b><br/>';
 if(isset($result['code'])) {
  echo '<b>Codice:</b>'.$result['code'].'<br/>';
 }
 echo '<b>Message:</b>'.urldecode($result['message']);
}
?>

 

 

 

Send.js file

var SMS_CLASSIC_MAX_LENGTH = 765,
SMS_BASIC_MAX_LENGTH = 1404,
CURRENT_SMS_LENGTH = 765;
var translations = Array();
translations['ErrorTextMoreThan']='Attenzione! Il testo non puo\' essere piu\' lungo di ';
translations['ErrorChars']=' caratteri!\n';
function addRecipient() {
var newRecipient = document.createElement('div');
newRecipient.innerHTML = '<input type="text" name="recipients[]" value="27" /> <a href="javascript:;" onclick="removeRecipient(this.parentNode);"><small>Remove</small></a>';
document.getElementById("recipients").appendChild(newRecipient);
}
function removeRecipient(obj) {
document.getElementById("recipients").removeChild(obj);
}
window.onload = function() {
var msgMaxLen = document.getElementById("messageMaxLength");
document.getElementById("method").onchange = function(){
 switch(this.value) {
  case "classic":
  case "report":
   msgMaxLen.innerHTML = SMS_CLASSIC_MAX_LENGTH;
   CURRENT_SMS_LENGTH = SMS_CLASSIC_MAX_LENGTH;
   charLeft();
   break;
  case "basic":
   msgMaxLen.innerHTML = SMS_BASIC_MAX_LENGTH;
   CURRENT_SMS_LENGTH = SMS_BASIC_MAX_LENGTH;
   charLeft();
   break;
 }
};
document.getElementById("text").onkeyup = function(){
 charLeft();
};
};
function charLeft(){
var max = CURRENT_SMS_LENGTH;
var smsText = document.getElementById("text").value;
var doubleChars = 0;
if(smsText==="") {
 var length = 0;
} else {
 var length = getRealLengthFor(smsText,false);
 doubleChars = getRealLengthFor(smsText,true);
}
if (length > max){
 document.getElementById("text").value = smsText.substring(0, max-doubleChars);
 alert(translations['ErrorTextMoreThan'] + max + translations['ErrorChars']);
}
var leftChars = length;
if (leftChars < 0){
 leftChars = 0;
}
document.getElementById("leftChars").innerHTML = leftChars;
var numberOfSMS = document.getElementById("numberOfSMS");
var actualNumberOfSMS = 0;
switch(max) {
 case SMS_CLASSIC_MAX_LENGTH:
  if(length<=160) {
   numberOfSMS.innerHTML = '1';
  }
  if(length>160) {
   actualNumberOfSMS = Math.floor((length-1) / 153) + 1;
   numberOfSMS.innerHTML = actualNumberOfSMS;
  }
  break;
 case SMS_BASIC_MAX_LENGTH:
  numberOfSMS.innerHTML = getNumberOfSegmentForBasic(smsText.length, smsText);
  break;
}
}
//from JAVA:
//var SPECIAL_CHARS = Array('[', '\\', ']', '^', '{', '|', '}', '~', '¬', 8364);
var SPECIAL_CHARS = Array("\x5B", "\x5C", "\x5D", "\x5E", "\x7B", "\x7C", "\x7D", "\x7E", 8364, 49792, 14844588);
var CHAR_TO_CUT_BASIC = Array(' ', '.', ',', '?', '!');
// Fa il check se una variabile e' null o vuota
function isEmpty( inputStr ) { if ( null == inputStr || "" == inputStr ) { return true; } return false; }
// Aggiungo un metodo alla classe string
// toCharArray esistente in java
String.prototype.toCharArray = function() {
var charArr=new Array();
for(var i=0;i<this.length;i++) charArr[i]=this.charAt(i);
return charArr;
};
// isIntInTheArray
// Cerca nell'array chars se e' presente il carattere c
// Parametri:
// char c, char[] chars
function isIntInTheArray(c, chars) {
for(var i=0;i<chars.length;i++) {
 if(chars[i] == c || chars[i] == c.charCodeAt(0)) return true;
}
return false;
}
// getRealLengthFor
// Parametri:
// String s
// Return int
function getRealLengthFor(s,getDoubleChars) {
if (isEmpty(s)) return 0;
var chars = s.toCharArray();
var numberOfSpecialChars = 0;
for (var k=0; k<chars.length; k++) {
 if (isIntInTheArray(chars[k], SPECIAL_CHARS)) {
  numberOfSpecialChars++;
 }
}
if(getDoubleChars === true) return numberOfSpecialChars;
return s.length + numberOfSpecialChars;
}
// Conteggio SMS Basic - Mirko Mariani
// Traduzione dall'algoritmo JAVA
// Return Int
// Parametri:
// Integer len, String s
function getNumberOfSegmentForBasic(len, s) {
if (len <= 160) return 1;
var leftStringToSplit = s;
var segmentNumber = 0, cycle = 0, segmentLength = 0;
var c = 'c';
var charForLeftString = null;
while (!isEmpty(leftStringToSplit)) {
 var indexOfEndSegment = -1;
 var numberOfSpecialChars = 0;
 if ( cycle == 0 ) {
  segmentLength = 156;
 }
 else {
  segmentLength = 156;
 }
 // Check if we reached the last segment
 if ( getRealLengthFor( leftStringToSplit , false) > segmentLength ) {
  charForLeftString = leftStringToSplit.toCharArray();
  for ( var k = 0; k < charForLeftString.length; k++ ) {
   if ( isIntInTheArray( charForLeftString[k], SPECIAL_CHARS ) ) {
 numberOfSpecialChars++;
   }
  }
  indexOfEndSegment = segmentLength - numberOfSpecialChars;
  // go to the point where cut the text
  var charChecked = 0;
  var charForCuttingFound = false;
  // search till the 15 char back
  while ( charChecked < 15 && !charForCuttingFound ) {
   c = leftStringToSplit.charAt( indexOfEndSegment - charChecked  );
   if ( isIntInTheArray( c, CHAR_TO_CUT_BASIC ) ) {
 segmentNumber++;
 charForCuttingFound = true;
   }
   else {
 charChecked++;
   }
  }
  // if no char has been found the string will be cut at the end of the segment length -> put the charChecked to 0 to
  // use the substring method below to determine the left string still to split
  if (!charForCuttingFound ) {
   segmentNumber++;
   charChecked = 0;
  }
  // change the left string still to split
  leftStringToSplit = leftStringToSplit.substring( indexOfEndSegment - charChecked, leftStringToSplit.length );
 }
 else {
  // reset to end the cycle
  leftStringToSplit = "";
  segmentNumber++;
 }
 cycle++;
}
return segmentNumber;
}

 

 

Validate.php

<?php
define("NET_ERROR", "Network+error+unable+to+send+the+message");
define("SENDER_ERROR", "You+can+specify+just+one+type+of+sender%2C+numeric+or+alphanumberic");
function do_post_request($url, $data, $optional_headers = null){
if(!function_exists('curl_init')) {
 $params = array(
  'http' => array(
   'method' => 'POST',
   'content' => $data
  )
 );
 if ($optional_headers !== null) {
  $params['http']['header'] = $optional_headers;
 }
 $ctx = stream_context_create($params);
 $fp = @fopen($url, 'rb', false, $ctx);
 if (!$fp) {
  return 'status=failed&message='.NET_ERROR;
 }
 $response = @stream_get_contents($fp);
 if ($response === false) {
  return 'status=failed&message='.NET_ERROR;
 }
 return $response;
} else {
 $ch = curl_init();
 curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
 curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
 curl_setopt($ch,CURLOPT_TIMEOUT,60);
 curl_setopt($ch,CURLOPT_USERAGENT,'Generic Client');
 curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
 curl_setopt($ch,CURLOPT_URL,$url);
 if ($optional_headers !== null) {
  curl_setopt($ch,CURLOPT_HTTPHEADER,$optional_headers);
 }
 $response = curl_exec($ch);
 curl_close($ch);
 if(!$response){
  return 'status=failed&message='.NET_ERROR;
 }
 return $response;
}
}
function VMGatewaySendSMS($username,$password,$recipients,$text,$sms_type='basic',$sender_number='',$sender_string='',$charset='',$optional_headers=null) {
$url = 'https://Myhostserver/WebsiteData/WebsiteDataEndpoint?wsdl';
switch($sms_type) {
 case 'classic':
  $method='send_sms_classic';
  break;
 case 'report':
  $method='send_sms_classic_report';
  break;
 case 'basic':
 default:
  $method='send_sms_basic';
}
$parameters = 'method='
  .urlencode($method).'&'
  .'username='
  .urlencode($username).'&'
  .'password='
  .urlencode($password).'&'
  .'text='
  .urlencode($text).'&'
  .'recipients[]='.implode('&recipients[]=',$recipients)
  ;
if($sender_number != '' && $sender_string != '') {
 parse_str('status=failed&message='.SENDER_ERROR,$result);
 return $result;
}
$parameters .= $sender_number != '' ? '&sender_number='.urlencode($sender_number) : '';
$parameters .= $sender_string != '' ? '&sender_string='.urlencode($sender_string) : '';
switch($charset) {
 case 'UTF-8':
  $parameters .= '&charset='.urlencode('UTF-8');
  break;
 case '':
 case 'ISO-8859-1':
 default:
}
parse_str(do_post_request($url,$parameters,$optional_headers),$result);
return $result;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/273624-sms-using-php-and-wsdl/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.