Version: 1.1

Type: Class

Category: Other

License: GNU Library Public License

Description: This is the PHP interface module for Voicent Gateway, a VoiceXML gateway for your PC. You can use this interface module to make telephone calls from your PHP script, provided that the Voicent Gateway is installed and can be accessed through HTTP. There is a FREE version of Voicent Gateway program downloadable from: http://www.voicent.com/download



Voicent Gateway PHP Interface

Voicent PHP class contains the following functions.

    call_text 
    call_audio 
    call_status 
    call_remove 
    call_till_confirm 

Since all these functions are implemented as a HTTP client communicating directly with Voicent Gateway, they can be run on any machine that has a connection to the host running the gateway. The php interface source code is included at the end of this section.


--------------------------------------------------------------------------------

SYNOPSIS

    call_text <phoneno> <text message> <selfdelete>
 
DESCRIPTION

Make a phone call and play the specified text message. The text message is convert to voice by Voicent Gateway's text-to-speech engine.
  
The options are: 
    <phoneno>        The phone number to call 

    <text message>   The message for the phone call 

    <selfdelete>     Ask the gateway to automatically delete the call request after the call is made if it is set to '1' 

    The return value is the call request id <reqId>. 

EXAMPLE

    call_text("123-4567", "Hello, how are you doing", 1); 

Make a call to phone number '123-4567' and say 'Hello, how are you doing'. Since the selfdelete bit is set, the call request record in the gateway will be removed automatically after the call.
  
    $reqId = call_text("123-4567", "Hello, how are you doing", 0);
  
Make a call to phone number '123-4567' and say 'Hello, how are you doing'. Since the selfdelete bit is not set, the call request record in the gateway will not be removed after the call. You can then use call_status to get the call status, or use call_remove to remove the call record.
  

--------------------------------------------------------------------------------

SYNOPSIS

    call_audio <phoneno> <audiofile> <selfdelete> 

DESCRIPTION

Make a phone call and play the specified audio message.
  
The options are: 
    <phoneno>     The phone number to call
 
    <audiofile>   The audio message for the phone call. The format must be PCM 16bit, 8KHz, mono wave file. The audio file must be on the same host as Voicent Gateway.
 
    <selfdelete>  Ask the gateway to automatically delete the call request after the call is made if it is set to '1' 

    The return value is the call request id <reqId>. 

EXAMPLE

    call_audio("123-4567", "C:\my audios\hello.wav", 1); 

Make a call to phone number '123-4567' and play the hello.wav file. Since the selfdelete bit is set, the call request record in the gateway will be removed automatically after the call.
  
    $reqId = call_audio("123-4567", "Hello, how are you doing", 0);
  
Make a call to phone number '123-4567' and play the hello.wav file. Since the selfdelete bit is not set, the call request record in the gateway will not be removed after the call. You can then use call_status to get the call status, or use call_remove to remove the call record.
  
  
--------------------------------------------------------------------------------

SYNOPSIS

    call_status <reqId> 

DESCRIPTION

Check the call status of the call with <reqId>. If the call is made, the return value is 'Call Made', or if the call is failed, the return value is 'Call Failed', and for any other status, the return value is "". 
EXAMPLE

    $status = call_status("11234035434"); 
  

--------------------------------------------------------------------------------

SYNOPSIS

    call_remove <reqId> 

DESCRIPTION

Remove the call record of the call with <reqId>. If the call is not made yet, it will be removed also. 

EXAMPLE

    call_remove("11234035434"); 
  

--------------------------------------------------------------------------------

SYNOPSIS

    call_till_confirm <vcast prog> <vcast doc> <confirmcode> <wavfile> 

DESCRIPTION

Keep calling a list of people until anyone enters the confirmation code. The message is the specified audio file. This is ideal for using it in a phone notification escalation process.

To use this feature, Voicent BroadcastByPhone Professional version has to be installed. This function is similar to the command line interface BroadcastByPhone has. But since the command cannot be invoke over a remote machine, this perl function uses the gateway to schedule an event, which in turn invokes the command on the gateway host.

The options are:

    <vcast prog>     The BroadcastByPhone program. It is usually 'C:\Program Files\Voicent\BroadcastByPhone\bin\vcast' on the gateway host. 

    <vcast doc>      The BroadcastByPhone call list to use. 

    <confirmcode>    The confirmation code use for the broadcast 

    <wavfile>        The audio file to use for the broadcast 

 

EXAMPLE

    call_till_confirm(
        "C:\Program Files\Voicent\BroadcastByPhone\bin\vcast.exe",
        "C:\My calllist\escalation.voc",
        "911911",
        "C:\My calllist\escalation.wav"); 

This will invoke BroadcastByPhone program on the gateway host and start calling everyone one the call list defined in 'C:\My calllist\escalation.voc'. The audio message played is 'C:\My calllist\escalation.wav'. And as soon as anyone on the list enters the confirmation code '911911', the call will stop automatically. 
  

--------------------------------------------------------------------------------

Source Code

<?php

class Voicent
{
    var $host = "localhost";
    var $port = 8155;

    function _call_now($body)
    {
        $headers = "POST /ocall/callreqHandler.jsp HTTP/1.1\r\n" .
                   "User-Agent: Mozilla/4.0\r\n" .
                   "Host: " . $this->host . "\r\n" .
                   "Content-Type: application/x-www-form-urlencoded\r\n" .
                   "Content-Length: " . strlen($body) . "\r\n" .
                   "\r\n";

        if (! ($sock = fsockopen($this->host, $this->port,          $errno, $errstr))) {
             echo $errstr;
             return false;
        }

        fwrite($sock, $headers.$body, strlen($headers.$body));

        $reqid = "";
        while ($line = fgets($sock, 4096)) {
            if (preg_match("/\[ReqId=(.*)\]/", $line, $matches)) {
                $reqid = $matches[1];
                break;
            }
        }

        fclose($sock);
        return $reqid;
    }

    function call_text($phoneno, $textmsg, $selfdelete)
    {
        $body = "info=" . urlencode("call " . $phoneno) . "&";
        $body .= "phoneno=" . urlencode($phoneno) . "&";
        $body .= "firstocc=10&";
        $body .= "txt=" . urlencode($textmsg) . "&";
        $body .= "selfdelete=" . $selfdelete;

        return $this->_call_now($body);
    }

    function call_audio($phoneno, $audiofile, $selfdelete)
    {
        $body = "info=" . urlencode("call " . $phoneno) . "&";
        $body .= "phoneno=" . urlencode($phoneno) . "&";
        $body .= "firstocc=10&";
        $body .= "audiofile=" . urlencode($audiofile) . "&";
        $body .= "selfdelete=" . $selfdelete;

        return $this->_call_now($body);
    }

    function call_status($reqId)
    {
        $headers = "GET /ocall/callstatusHandler.jsp?reqid=" . $reqId . " HTTP/1.1\r\n" .
                "User-Agent: Mozilla/4.0\r\n" .
                "Host: " . $this->host . "\r\n" .
                "\r\n";

        if (! ($sock = fsockopen($this->host, $this->port, $errno, $errstr))) {
            echo $errstr;
            return false;
        }

        fwrite($sock, $headers, strlen($headers));

        $status = "";
        while ($line = fgets($sock, 4096)) {
            if (preg_match("/\^made\^/", $line)) {
                $status = "Call Make";
                break;
            }
            if (preg_match("/\^failed\^/", $line)) {
                $status = "Call Failed";
                break;
            }
        }

        fclose($sock);
        return $status;
    }

    function call_remove($reqId)
    {
        $headers = "GET /ocall/callremoveHandler.jsp?reqid=" . $reqId . " HTTP/1.1\r\n" .
                "User-Agent: Mozilla/4.0\r\n" .
                "Host: " . $this->host . "\r\n" .
                "\r\n";

        if (! ($sock = fsockopen($this->host, $this->port, $errno, $errstr))) {
            echo $errstr;
            return false;
        }

        fwrite($sock, $headers, strlen($headers));

        $line = fgets($sock, 4096);

        fclose($sock);
    }

    function call_till_confirm($vcast, $voc, $confirmcode, $wavefile)
    {
        $body = "info=" . urlencode("call till confirm") . "&";
        $body .= "phoneno=0000000" . "&";
        $body .= "firstocc=10&";
        $body .= "startexec=" . urlencode($vcast) . "&";
        $body .= "selfdelete=" . $selfdelete . "&";

        $cmdline = "\"" . $voc . "\" -startnow -confirmcode " . $confirmcode;
        $cmdline .= " -wavfile \"" . $wavefile . "\"";
        $body .= "cmdline=" . urlencode(cmdline);

        return $this->_call_now($body);
    }
}

# $voicent = new Voicent;
# $id = $voicent->call_text("1234567", "hello, how are you", "0");
# print $id;
# $id = $voicent->call_audio("1234567", "C:\Temp\welcome.wav", 0);
# print $id;
# print $voicent->call_status("1103704332609");
# $voicent->call_remove("1103704332609");
# print $voicent->call_status("1103704332609");

?>