IVR with BAckground doesn't work with php

Hello everbody,

I have a probably very simple probleme. I have a script to make make sound file. It works perfect with STREAM FILE but not with BACKGROUND

in my php file

ini_set(‘display_errors’, ‘1’);

turn off output buffering


don’t let this script run for more than 60 seconds


turn off error reporting, as it will most likely interfere with

the AGI interface


function execute_agi($command) {
fwrite(STDOUT, “$command\n”);
$result = fgets(STDIN);
$ret = array(‘code’=> -1, ‘result’=> -1, ‘timeout’=> false, ‘data’=> ‘’, ‘command’=>$command);
if (preg_match("/^([0-9]{1,3}) (.)/", $result, $matches)) {
$ret[‘code’] = $matches[1];
$ret[‘result’] = 0;
if (preg_match('/^result=([0-9a-zA-Z]
)(?:\s?((.*?)))?$/', $matches[2], $match)) {
$ret[‘result’] = $match[1];
$ret[‘timeout’] = ($match[2] === ‘timeout’) ? true : false;
$ret[‘data’] = $match[2];
return $ret;

//$result = execute_agi(‘BACKGROUND(’.$pathfichier.')"); //doesn’t work
$result = execute_agi(‘STREAM FILE ‘.$pathfichier.’ “”’); //work

but if i put manualy the background file in the IVR, it works… why ?

exten => s,n,Background(/var/lib/asterisk/agi-bin/sound/b637ea6a12d670b34a0562116b783eec)

probably something incredubly stupid but i don’t find.

Any idea ?


Use fully rooted path to file ?

BACKGROUND is not a valid AGI command. If you want to execute arbitrary dialplan applications you would use EXEC[1], though whether that works with Background I’m not sure.

[1] EXEC - Asterisk Documentation

Background and WaitExten are not well behaved commands. They integrate tightly with the dialplan interpreter; They basically return the first digit in a special way that causes the dialplan interpreter to go into its incomplete number logic and look for further digits.

There is some special handling for AGI the details of which I forget, but basically, to work properly, they have to be called directly from the dialplan, so that they return to it once they have a digit.

thanks for so many answer.

i already triee full path, it doesn t work

about exec , i will try. if i understand you , i should simply add exec in the output of my php script ?


david, i m sure it is possible … but there “something”… but what…?

You are great the solution was execute_agi(‘EXEC BACKGROUND ‘.$pathfichier.’’);

Joshua, you win a beer when you will come in Belgium

But can you explain me why it works ? Why do we need to put this “exec” in a php script and not when we do it directly in the AGI ?

Where can i find more info about it ?

Do what directly in the AGI?

AGI has specific commands that it supports. They aren’t dialplan applications. To execute a dialplan application, you use the EXEC AGI command.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.