Send SMS Form sample

From YateBTS
Jump to: navigation, search

Here is an example of how to talk from a middleware PHP application to YateSMSC to send a SMS to a subscriber.

The code is divided in two parts: an utility function that can be used as-is and a sample HTML form.

The library

This is the utility function that hides all the JSON API interaction.

You may save the PHP code below to a file named ysmsc_send.php

<?php

/* ysmsc_send.php
 * This file is part of the YATE Project http://YATE.null.ro
 *
 * JSON over HTTP API simple SMS send function for YateSMSC
 *
 * Yet Another Telephony Engine - a fully featured software PBX and IVR
 * Copyright (C) 2017 Null Team
 *
 * This software is distributed under multiple licenses;
 * see the COPYING file in the main directory for licensing
 * information for this specific distribution.
 *
 * This use of this software may be subject to additional restrictions.
 * See the LEGAL file in the main directory for details.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */


/**
 * Submit a SMS to YateSMSC using a JSON/HTTP request, requires php-curl
 * @param $url URL of the YateSMSC API
 * @param $orig Originator MSISDN or short alphanumeric string
 * @param $dest Destination MSISDN
 * @param $text Text of the short message
 * @param $tout Optional timeout, defaults to 5 seconds
 * @return Error text, null on success
 */
function sendSmsJson($url,$orig,$dest,$text,$tout = 5)
{
    $url = trim($url);
    $orig = trim($orig);
    $dest = trim($dest);
    if (!$url || "" == $orig || "" == $dest || "" == $text)
	return "Missing one or more parameters";
    if (!preg_match('/^[+]?[1-9][0-9]+$/',$dest))
	return "Invalid destination number";

    $params = array(
	"originator" => $orig,
	"destination" => $dest,
	"text" => $text
    );
    if (!preg_match('/^[+]?[1-9][0-9]+$/',$orig))
	$params["orignumtype"] = "alphanumeric";
    else if (strlen($orig) < 5)
	$params["orignumtype"] = "unknown";
    $body = json_encode(array(
	"request" => "schedule_sms",
	"node" => "smsc",
	"params" => $params
    ));

    $curl = curl_init($url);
    if (false === $curl)
	return "cURL initialization failure";
    curl_setopt($curl,CURLOPT_POST,true);
    curl_setopt($curl,CURLOPT_POSTFIELDS,$body);
    curl_setopt($curl,CURLOPT_HTTPHEADER,array(
	"Content-Type: application/json",
	"Accept: application/json,text/x-json",
	"Expect:"
    ));
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,$tout);
    curl_setopt($curl,CURLOPT_TIMEOUT,$tout);
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
    $body = curl_exec($curl);

    if (false === $body)
	$body = curl_error($curl);
    else {
	$info = curl_getinfo($curl,CURLINFO_HTTP_CODE);
	if (200 != $info)
	    $body = "HTTP returned code: $info";
	else {
	    $info = curl_getinfo($curl,CURLINFO_CONTENT_TYPE);
	    if (false === strpos($info,"json"))
		$body = "Unexpected MIME type: $info";
	    else {
		$body = json_decode($body,true);
		if (isset($body["scheduled_sms"]))
		    $body = null;
		else if (isset($body["code"])) {
		    if (isset($body["message"]))
			$body = "API error " . $body["code"] . ": " . $body["message"];
		    else
			$body = "API error code " . $body["code"];
		}
		else if (isset($body["message"]))
		    $body = "API error: " . $body["message"];
		else
		    $body = "Unknown API error";
	    }
	}
    }
    curl_close($curl);
    return $body;
}

/* vi: set ts=8 sw=4 sts=4 noet: */
?>

Sample form

This is just a short example on how to actually call the sendSmsJson() function.

You may save the PHP code below to a file named send_sms.php placed in the same directory as ysmsc_send.php on the Web server.

<?php

/* Sample form for sending a SMS to YateSMSC */

require_once("ysmsc_send.php");

$url = isset($_POST["url"]) ? $_POST["url"] : "";
$orig = isset($_POST["orig"]) ? $_POST["orig"] : "";
$dest = isset($_POST["dest"]) ? $_POST["dest"] : "";
$text = isset($_POST["text"]) ? $_POST["text"] : "";
$res = "";
if ("" != $url && "" != $orig && "" != dest && "" != $text) {
    $res = sendSmsJson($url,$orig,$dest,$text);
    if ($res)
	$res = htmlspecialchars($res);
    else
	$res = "OK";
}
?><html>
<head>
<title>Test sending SMS</title>
</head>
<body bgcolor="#e0e0e0">
<h1>Test sending SMS</h1>
<form method="POST">
<table border="0" bgcolor="#c0c0c0">
<tr><th>API URL:</th><td colspan="2"><input type="text" name="url" size="64" value="<?php print "$url"; ?>" /></td></tr>
<tr><th>Originator:</th><td colspan="2"><input type="text" name="orig" size="16" value="<?php print "$orig"; ?>" />
(number or short alphanumeric)</td></tr>
<tr><th>Destination:</th><td><input type="text" name="dest" size="16" value="<?php print "$dest"; ?>" />
(number only)</td><td align="right"><input type="submit" /></td></tr>
<tr><th>SMS text:</th><td colspan="2"><textarea name="text" rows="30" cols="64"><?php print "$text"; ?></textarea></td></tr>
<tr><th>Result:</th><td colspan="2"><?php print "$res"; ?></td></tr>
</table>
</form>
</body>
</html>

The result

Point your browser to the send_sms.php on the server and you should see the form.

Fill the fields and click on Submit. Please note that an alphanumeric originator can be at most 11 characters long as it needs to fit in 10 octets after GSM-7 packing.

Ysmsc send.png

If the result is OK the short message was queued in the SMSC.

JSON Request

Here is the JSON API communication as logged by YateSMSC:

------ 2017-06-21 17:22:08, ip=xxx.xxx.xxx.xxx
Json: {"request":"schedule_sms","node":"smsc","params":{"originator":"Operator","destination":"+88270053","text":"Hello\r\n  there!","orignumtype":"alphanumeric"}}}
JsonOut: {"code":0,"scheduled_sms":1}

SMSC CDR

And here is how the SMS looks like in the YateSMSC's CDR:

Time            Typ  Node  Billing ID     Prot  Address or GT          Caller    Called    Durat  Directn   IMSI
--------------  ---  ----  -------------  ----  ---------------------  --------  --------  -----  --------  ---------------
1498054928.666  msg  SMSC  1496685302-48  HTTP  xxx.xxx.xxx.xxx:38268  Operator  88270053  0.011  incoming
1498054929.492  msg  SMSC  1496685302-48  MAP   882xxxxxxx             Operator  88270053  5.301  outgoing  00101xxxxxxxxxx