[asterisk-users] PHP AGI Problems

D Tucny d at tucny.com
Wed May 27 03:03:10 CDT 2009


2009/5/27 Atlanticnynex <atlanticnynex at gmail.com>

> (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'.
>

It's not... There are problems with the way you are handling the return
data...


>
> Also, GET DATA seems to be returning early and the loop keeps
> prompting 'invalid'...


That's happening because you are sending junk to asterisk...


>
> Any suggestions on how to improve my AGI class so it actually works?
>

Have a look at http://www.voip-info.org/wiki/view/Asterisk+AGI+php...


>
> Thanks.
>
> [code]

<snip>


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

>   $msg_id = $result[result];

Problem here, you're looking to extract the value for key result (which as
it's not quoted could be interpreted as a constant) from the array $result,
but, $result isn't an array


>   if ($msg_id < 1000){
>       echo "\ndebug: msgid < 1000, invalid ($msg_id)!\n";

Junk is being sent to asterisk which is responding with errors (echo sends
to STDOUT too), you're only getting one line at a time of response, so,
after the first time in you are not getting the results you'd expect in the
GET DATA, you're getting the rest of the error messages from the junk you
sent...

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

<snip>


>
>   function send_cmd($cmd){
>       fputs(STDOUT, $cmd . " \n");
>       fflush(STDOUT);
>       $data = fgets(STDIN, 4096);
>       return $data;
>   }
>
So, as said above, you're sending the command here, then taking the next
line from asterisk and dumping it back unprocessed in any way...

When you're sending the GET DATA command, if you enter 3333, what you are
getting back is...

'200 result=3333\n'

$result actually contains this value, however, when you set $msg_id to
$result[result] what you get is the first character in $msg_id, so, in this
case you get '2'...

As this is lower than 1000 you go into the invalid number part of your code
where you echo a debug message then send the command to play the invalid
sound file... Sending the command to play the sound file results in a 1 line
response being received, your echo command put out two invalid lines, so,
what is actually received as a response this time is...

'510 Invalid or unknown command\n'

The next command you send is the GET DATA command to try and get a new
number, however, at this point there is already data waiting in the buffer,
so as soon as the data is sent, your fgets returns straight away with...

'510 Invalid or unknown command\n'

as a result of the second line of debug output that you sent...

$result contains this string and again, you get the first character in
$msg_id, so now $msg_id equals '5', this again is lower than 1000, so you go
into the invalid number part of your code and drop more junk into asterisk
and continue...

I hope that's useful in explaining the problems you are experiencing... as I
said above, check out the page on voip-info, it should be enough for you to
get this fixed up nicely... One think you'll notice about the examples on
there, debug output is sent to a file, that way you can record information
for debugging without polluting your agi...

d
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.digium.com/pipermail/asterisk-users/attachments/20090527/b416e9ac/attachment.htm 


More information about the asterisk-users mailing list