[asterisk-users] PHP AGI Problems

Atlanticnynex atlanticnynex at gmail.com
Wed May 27 01:10:01 CDT 2009


(Accidentally posted this to asterisk-dev, should be here)

fgets is only returning one character... either when run as an AGI or
run as a test on PHP on CLI...
Example, enter 3333, then fgets returns '3'.

Also, GET DATA seems to be returning early and the loop keeps
prompting 'invalid'...
Any suggestions on how to improve my AGI class so it actually works?

Thanks.

[code]


#!/usr/bin/php
<?
//playspecific.php
require_once('database.class.php');
require_once('AGI.class.php');

$database = new database();
$AGI = new AGI();

$database->selectdb("switchboard");

while ($error_count < 5 && !$msg_result) {
   $result = $AGI->send_cmd("GET DATA /bswitch/menu/enter-msg-id 3000 4");
   $msg_id = $result[result];

   if ($msg_id < 1000){
       echo "\ndebug: msgid < 1000, invalid ($msg_id)!\n";
       //var_dump($msg_id);
       $error_count += 1;
       $AGI->send_cmd("EXEC PLAYBACK /bswitch/menu/invalid-msg-id");
       }
   else{
       echo "\ndebug: ($msg_id) okay valid msgid, lets check sql\n";
       $msg_result=check_msg($msg_id);
       if(!$msg_result){
       $error_count +=1;
       }
   }
}

$row = mysql_fetch_array($messageresult);
$AGI->send_cmd("EXEC CONTROL STREAM FILE $row[path]");
$AGI->send_cmd("EXEC PLAYBACK beep");


function check_msg($ID){
   $msg_result=$database->querydb("**********sql query filtered**********");
   return $msg_result;
}

?>






<?php
//AGI.class.php
class AGI {
   public $agivars;

   function _get_agivars(){
       $agivars = array();
       while (!feof(STDIN)) {
           $agivar = trim(fgets(STDIN));

           if ($agivar === '') {
               break;
           }

           $agivar = explode(':', $agivar);
           $agivars[$agivar[0]] = trim($agivar[1]);
       }
       return 0;
   }

   function send_cmd($cmd){
       fputs(STDOUT, $cmd . " \n");
       fflush(STDOUT);
       $data = fgets(STDIN, 4096);
       return $data;
   }

   function __construct(){
       $this->_get_agivars();
   }

}
?>



[/code]



More information about the asterisk-users mailing list