Queue + AGI Script

So i tried using one of the examples liste don that page - When run - the CLI shows:
– Launched AGI Script /var/lib/asterisk/agi-bin/test3.agi
– AGI Script test3.agi completed, returning 0

The actual code:

#!/usr/bin/php -q
<?php
ob_implicit_flush(true); 
set_time_limit(6); 
error_reporting(0);  //added by user: democritus 
$in = fopen("php://stdin","r"); 
$stdlog = fopen("/var/log/asterisk/my_agi.log", "w"); 

// toggle debugging output (more verbose) 
$debug = true; 

// Do function definitions before we start the main loop 
function read() { 
  global $in, $debug, $stdlog; 
  $input = str_replace("\n", "", fgets($in, 4096)); 
  if ($debug) fputs($stdlog, "read: $input\n"); 
  return $input; 
} 

function write($line) { 
  global $debug, $stdlog; 
  if ($debug) fputs($stdlog, "write: $line\n"); 
  echo $line."\n"; 
} 

// parse agi headers into array 

while ($env=read()) { 
  $s = split(": ",$env); 
  $agi[str_replace("agi_","",$s[0])] = trim($s[1]); 
  if $env == "")  { 
    break; 
  } 
} 


 write("GET VARIABLE foo"); 
 $a = read(); 

echo "VERBOSE \"$a\" \n"; 
read(); 


// clean up file handlers etc. 
fclose($in); 
fclose($stdlog); 
exit; 

?>  

[quote=“davevg”]I’m pretty sure the issue you are having here is that you need to read in all of the buffered input prior to doing your write. The agi process sends a bunch of variables on startup.
The following link gives an example.
voip-info.org/tiki-index.php … 0AGI%20php[/quote]

This is a very good point. You need to read everything up to the point of your write before you try and read again otherwise the internal php pointer will be in the ‘wrong’ place.

Don’t forget ‘set agi debug on’ so you can see what your script is doing.

I tried doing a read(); before i did any write. No joy.

Yes Debug is on.

I am literally using the code from section 14 (voip-info.org/tiki-index.php … sampleread)

Also as a side note, its not writing anything to the agi log file.

I’m pretty stumped here :S

Just a friendly bump. If anyone has any ideas - it would be most appreciated!

This code is tested and known to work…

#!/usr/bin/php -q
<?php
  
 ob_implicit_flush(true);
 set_time_limit(6);
 $stdin = fopen('php://stdin', 'r');
 $stdout = fopen('php://stdout', 'w');
 $stdlog = fopen('/var/log/asterisk/agi', 'w');
 
 // toggle agi log debugging output (more verbose)
 $debug = true;

 // Do function definitions before we start the main loop
 // Functions for variable input / output
 
 function read() {
	
	global $stdin, $stdlog, $debug;
	
	$input = str_replace("\n", "", fgets($stdin, 4096));
	if ($debug) {
		fputs($stdlog, "".date("H:i:s")." Read: $input\n");
	}
	
	return $input;
 }

 function write($line) {
 
	global $stdout, $stdlog, $debug;
	
	if ($debug) {
		fputs($stdlog, "".date("H:i:s")." Write: $line\n");
	}
	
	echo $line."\n";
	
 }

 function errlog($line) {
 
	global $err, $stdlog, $debug;
	
	echo "VERBOSE \"$line\" \n";
	if ($debug) {
		fputs($stdlog, "".date("H:i:s")." Msg: $line\n");
	}
	
	read();
 }
 
	// MAIN PROGRAM
	// Parse agi headers sent from Asterisk into an array
	while ($env=read()) {
		$s = split(": ",$env);
		$agi[str_replace("agi_","",$s[0])] = trim($s[1]);
		if (($env == "") || ($env == "\n")) {
			break;
		}
	}
	
	// Get the variable I set in Asterisk
	write("GET VARIABLE myvar");
	$myVarInPhp = read();
	$stringArray = explode("(", $myVarInPhp);
	$string = substr($stringArray[1], 0, -1);
	echo "VERBOSE \"$string\" \n";
	
 ?>

All you have to do is set the variable in Asterisk and call the AGI script…

The ael way…
myvar=testvariable;
AGI(testing.agi);

HTH 8)

This worked like a charm! Thank you Wimnat! One last problem I cannot seem to be able to pass more than one variable through - How do you pass more than one?

Pass variables through? You mean when you call your AGI script? Just use comma seperated values…

AGI(testing.agi,myvar1,myvar2);

Then in your AGI script you can access them in your $agi array…

$arg1 = $agi[‘arg_1’];
$arg2 = $agi[‘arg_2’];

Sorry what I meant to say was how do I perform a GET on multiple variables?

// Get the variable I set in Asterisk
write(“GET VARIABLE foo,foo2”);
$myVarInPhp = read();
$stringArray = explode("(", $myVarInPhp);
$string = substr($stringArray[1], 0, -1);
$string2 = substr($stringArray[2], 0, -1);
echo “VERBOSE “$string” \n”;
echo “VERBOSE “$string2” \n”;

something like this?

Unfortunately you can’t.

You’ll have to GET and read() each variable on its own. Be sure to read() and store the variable before performing your next GET action.

Still unable to pull a second variable :frowning:

// Get the variable I set in Asterisk
write(“GET VARIABLE foo”);
$myVarInPhp = read();
$stringArray = explode("(", $myVarInPhp);
$string = substr($stringArray[1], 0, -1);
echo “VERBOSE “$string” \n”;

// Get the second variable I set in Asterisk
write(“GET VARIABLE foo2”);
$myVarInPhp = read();
$stringArray = explode("(", $myVarInPhp);
$string = substr($stringArray[1], 0, -1);
echo “VERBOSE “$string” \n”;

Is that supposed to work? Please advise.

You’ve ran an asterisk command ‘VERBOSE’ without reading its return value inbetween your two GET commands. When you try and read your GET command it’s actually reading your VERBOSE command.

Easy fix. Just put read() after echo “VERBOSE “$string” \n”;

Easier fix. Use errlog($string). The errlog function that i wrote automatically does a read() every time it’s called so you don’t have to worry about the read() every time.

I suggest you to try the php class phpagi from phpagi.sourceforge.net, should be more easy to interact with * through agi.

Cheers.

Marco Bruni
www.marcobruni.net