Change messages with a PHP script

In the basic- and full editions of Diafaan SMS Server it is possible to block or change individual messages with an HTTP call to a script on an external web server. But the HTTP callback function can also be configured to run a script on a locally installed instance of PHP. This is faster than running the script on a remote web server and it is an easy way to manipulate the message parameters or block certain messages. The HTTP callback function can also be used with a local C# script, which is much faster than a PHP script locally.

Installation of PHP

PHP can be downloaded from the PHP website. For this article we downloaded the Non Thread Safe version of PHP 7.3.5 for Windows and unzipped the file in folder 'C:\Program Files\PHP\php-7.3.5'. As long as we do not need external resources, like a database, this is all the installation that is required.

Enable HTTP callbacks

Set up HTTP callbacksSet up HTTP callbacks

The next step is to enable HTTP callbacks in Diafaan SMS Server and set it up to use the PHP program. The HTTP callbacks settings are available in the general options: 'Actions-Options-HTTP callbacks'.

For use with PHP the 'Callback method' must be set to 'CGI application' or 'FastCGI application'. The first option is more secure because it starts a separate PHP process for each call. FastCGI is much faster because it uses one PHP process for all calls and avoids the overhead to start a process for each message.

The 'CGI application' parameter must be set to point to the php-cgi.exe program that was just installed and this is also a good time to create an empty PHP script file and set the 'Script file' parameter to point to this file.

Edit the script file

Diafaan SMS Server now calls the PHP script for every message that a connector wants to send, each message that a gateway receives and for each messages that is written to the log or receives a status update. The empty script file will block each message, the minimum script we can write to the script file is a script that returns a 'result=1' response so that each message will be handled normally by Diafaan SMS Server:

<?php
	echo 'result=1';
?>

Information about the parameters that can be used in the HTTP callback scripts is available in the online manual.

Script to change the message text

The following script changes the message text for each outgoing SMS message, it does not change received SMS messages and log updates:

<?php
if ($_REQUEST['action'] == 'message_out') {
  $data = array(
      'result' => '1',
      'message' => 'PHP script: '.$_REQUEST['message']
  );
  echo http_build_query($data);
}
else {
	echo 'result=1';
}
?>

Script to allow only French destination numbers

This script blocks all messages without a French destination number:

<?php
if ($_REQUEST['action'] == 'message_out') {
  if ((strpos($_REQUEST['to'], "+33") === 0) || (strpos($_REQUEST['to'], "33") === 0)) {
  	echo 'result=1';
  }
  else {
  	echo 'result=0';
  }
}
else {
	echo 'result=1';
}
?>

Script to block all inbound messages except from one number

This script blocks all inbound messages except messages from number +49111111111:

<?php
if ($_REQUEST['action'] == 'message_in') {
  if ($_REQUEST['from'] == "+49111111111") {
    echo 'result=1';
  }
  else {
    echo 'result=0';
  }
}
else {
    echo 'result=1';
}
?>

Script to change the 'from' number format of inbound messages

This script removes the leading '+' characters of the sender phone number of inbound messages:

<?php
if ($_REQUEST['action'] == 'message_in') {
    echo 'result=1'.chr(38).'from='.urlencode(ltrim($_REQUEST['from'], '+'));
}
else {
    echo 'result=1';
}
?>