[Freeswitch-users] Problem accessing channel variables

Richard Gration richgration at gmail.com
Wed Nov 28 13:54:34 MSK 2012


Hi all,

I'm trying to write something to fit into a two factor auth mechanism.
I have a CGI script which is called with a phone number and a PIN as
parameters. In the script I use the event socket api to place a call.
The PHP code I'm using to write to the socket is straight out of the
FS docs (can't remember exactly, I think I got it from the wiki). This
is my api command:

api originate {originate_timeout=30}sofia/external/${dExt}@MGC
'&play_and_get_digits(4 4 2 30000 # torvalds-says-linux.wav
torvalds-says-linux.wav foo \d+)'

This all works fine, up to a point. The call is made, the wav is
played. But then PAGD is supposed to put the digits into a channel
var, in this case "foo". But I've been having trouble getting the
value of this channel var *reliably*. If the # key is used to
terminate the input, then I can get the channel var value with

api uuid_getvar $uuid foo

However, if the input is terminated by PAGD because 4 digits have been
entered, then the call is hung up (by FS/PAGD) before I have a chance
to get the value of the channel var. Can anyone help here please? I
suppose I can work around this by increasing the digits to 5 and
telling people to use # to terminate the input, but I'd like to
understand the correct way to use PAGD if possible.

If you think I'm going about this the wrong way entirely, then please
suggest an alternative :-)

My code is below. As I say, lines 1-71 are straight out of the docs.

  1 <?php
  2
  3  $password = "xxxxxx";
  4  $port = "xxxx";
  5  $host = "127.0.0.1";
  6
  7  function event_socket_create($host, $port, $password) {
  8       $fp = fsockopen($host, $port, $errno, $errdesc)
  9          or die("Connection to $host failed");
 10       socket_set_blocking($fp,false);
 11
 12       if ($fp) {
 13             while (!feof($fp)) {
 14                 $buffer = fgets($fp, 1024);
 15                 usleep(100); //allow time for reponse
 16                 if (trim($buffer) == "Content-Type: auth/request") {
 17                     fputs($fp, "auth $password\n\n");
 18                     break;
 19                 }
 20             }
 21             return $fp;
 22       }
 23       else {
 24             return false;
 25       }
 26  }
 27
 28  function event_socket_request($fp, $cmd) {
 29
 30       if ($fp) {
 31             fputs($fp, $cmd."\n\n");
 32             usleep(200); //allow time for reponse
 33
 34             $response = "";
 35             $i = 0;
 36             $contentlength = 0;
 37             while (!feof($fp)) {
 38                 $buffer = fgets($fp, 4096);
 39                 if ($contentlength > 0) {
 40                     $response .= $buffer;
 41                 }
 42
 43                 if ($contentlength == 0) { //if contentlenght is
already don't process again
 44                      if (strlen(trim($buffer)) > 0) { //run only
if buffer has content
 45                           $temparray = split(":", trim($buffer));
 46                           if ($temparray[0] == "Content-Length") {
 47                               $contentlength = trim($temparray[1]);
 48                           }
 49                      }
 50                 }
 51
 52                 usleep(200); //allow time for reponse
 53
 54                 //optional because of script timeout //don't let
while loop become endless
 55                 if ($i > 90000) { break; }
 56
 57                 if ($contentlength > 0) { //is contentlength set
 58                      //stop reading if all content has been read.
 59                      if (strlen($response) >= $contentlength) {
 60                          break;
 61                      }
 62                 }
 63                 $i++;
 64             }
 65
 66             return $response;
 67       }
 68       else {
 69          echo "no handle";
 70       }
 71  }
 72
 73  function strip_cr($str) {
 74     return preg_replace("/\n/",'',$str);
 75  }
 76
 77  $dExt = $_GET['dExt'];
 78  if ($dExt) {
 79     $fp = event_socket_create($host, $port, $password);
 80
 81     # Place call and find uuid
 82     $cmd = "api originate
{originate_timeout=30}sofia/external/677${dExt}@194.145.191.131
'&play_and_get_digits(4 4 2 30000 # torvalds-says-linux.wav
torvalds-says-linux.wav foo \d+)'";
 83     $response = event_socket_request($fp, $cmd);
 84     $response = preg_replace('/</','&lt;',$response);
 85     $response = preg_replace('/>/','&gt;',$response);
 86     $response = strip_cr($response);
 87     $bits = preg_split('/\s+/',$response);
 88     $uuid = $bits[1];
 89
 90     echo '<pre>';
 91     echo date('H:i:s') . "\n";
 92     echo "call placed: $cmd\n";
 93     echo "response :$response:\n";
 94     echo "uuid $uuid\n";
 95
 96     # Retrieve channel variable
 97     $cmd = "api uuid_getvar $uuid foo";
 98     $response = '';
 99     while (! $response or $response == '_undef_') {
100         echo "in loop : got _undef_\n";
101         $response = event_socket_request($fp, $cmd);
102         $response = strip_cr($response);
103     }
104     echo "response :$response:\n";
105     echo '</pre>';
106
107     # Close socket
108     fclose($fp);
109  } else {
110     echo 'Request URL in this format: /test.php?dExt=<phone_number>' . "\n";
111  }
112
113  ?>


Cheers,
Rich

-- 
Once our basic material needs are met - in my utopia, anyway - life
becomes a perpetual celebration in which everyone has a talent to
contribute. But we cannot levitate ourselves into that blessed
condition by wishing it. We need to brace ourselves for a struggle
against terrifying obstacles, both of our own making and imposed by
the natural world. And the first step is to recover from the delusion
that is positive thinking.
       -- Barbara Ehrenreich



Join us at ClueCon 2011 Aug 9-11, 2011
More information about the FreeSWITCH-users mailing list