Problem with calling AGI commands from PHP

Below is a functional extract from a script I’m having problems with, it’s cropped down to show the code I’m using and demonstrate the problem.

The expected result is the welcome message to be played 3 times, so you would hear “Welcome Welcome Welcome” but instead, it is played only once. If I put pauses between the lines then the are all played, but I’m going to be using text-to-speech and I wont know how long each time is going to be.

I need to script to wait until it’s finished playing one string before it starts playing the next.
Please help.

[code:1]#!/usr/bin/php -q

<?php ob_implicit_flush(false); $stdin = fopen( 'php://stdin', 'r' ); $stdout = fopen ('php://stdout', 'w' ); $rc = execute_agi("STREAM FILE welcome \"\" "); $rc = execute_agi("STREAM FILE welcome \"\" "); $rc = execute_agi("STREAM FILE welcome \"\" "); function execute_agi($command) { GLOBAL $stdin, $stdout; fputs ($stdout, $command ."\n" ); fflush ($stdout); } sleep (5); ?>

[/code:1]