Search code examples
phpgetsmsfreepbx

Send SMS from PHP script with FreePBX Bootstrap


In the below script, which was written by an unknown third party, the only changes I've made were changing the value of $to, $from, and $body from hard-coded strings to $_GET elements. The aim of the project is to pass some parameters to this script in a query string, and then compose and send and SMS message with it.

<?php

/**** **** **** **** **** **** **** **** **** **** **** 
 * sms.php 
 * 
 * sample PHP code to send an SMS message to a registered 
 * extension on a FreeBX 12 server 
 * 
 * version history
 *    2015-09-22   version 0 by [email protected]
 **** **** **** **** **** **** **** **** **** **** ****/

// Load the FreeBPX bootstrap, requires FreePBX 12
if (!@include_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) {
    include_once('/etc/asterisk/freepbx.conf');
}

// The Asterisk Manager Class from the boostrap is $astman 
// If using FreePBX 13+ must set asman with 
//  $astman = new AGI_AsteriskManager( );
if ($astman) {
    // $to = "sip:#######";
    // $from = '"Caller ID Name" <#######>';
    // $body = "cats are yummy";
    $to = $_GET['to'];
    $from = $_GET['from'];
    $body = $_GET['body'];

    $result = $astman->MessageSend($to, $from, $body);

    print_r($result);   //debug

    // the variable $result will be an array of the formats
    // Array ( [Response] => Success [Message] => Message successfully sent )
    // Array ( [Response] => Error [Message] => Message failed to send. )
} else {
    echo "No Asterisk Manager Connection";
}

However, even though this script works fine with those commented-out hard-coded values, changing those values to $_GET elements results in this error message:

Array ( [Response] => Error [Message] => Message technology not found. )

I'm trying to find some sort of documentation to explain to me how this works... Any ideas from anyone else who's worked with FreePBX bootstrap?


Solution

  • The solution to this problem was not in the encoding of the document, or the data, or that maybe this API doesn't play well with query strings (all three of which crossed my mind at some point throughout the day), but the secret lay in the format of the data being passed to the api.

    The answer was sitting under my nose the whole time:

    // $to = "sip:#######";
    // $from = '"Caller ID Name" <#######>';
    // $body = "cats are yummy";
    $to = $_GET['to'];
    $from = $_GET['from'];
    $body = $_GET['body'];
    

    So, here was the solution to the problem:

    $to = 'sip:' . $_GET['to'];
    $from = '"Caller ID Name" <' . $_GET['from'] . '>';
    $body = $_GET['body'];
    

    You can pass your data via the URL like this:

    http://www.url.com/sms.php?to=0000000&from=0000001&body=hello