从AGI调用PHP函数不起作用

I'm using PHPAGI script telling the user to dial a number and then pass the number to the function to dial out, but it isn't working. I'm trying to call the function from my script but it isn't working.

Here is my code:

$agi->text2wav("please enter number to dial");
$num = $agi->exec("Read","pin,,,3,120");
$temp = $agi->get_variable("pin");
$pin = trim($temp["data"]);
dialout("$pin");

function dialout($pin)
{
    $agi=new AGI();
    $agi->exec_dial("SIP/$pin");
    $Answeredtime = $agi->get_variable("ANSWEREDTIME");
    $agi->hangup ();
}

1) stop asterisk

asterisk -rx " core stop now"

2) start asterisk at current console

asterisk -vvvvgc

NOT stop it.

3) issue

 agi set debug on

After that you will have much more info and php errors from your script