Incoming calls and execution of PHP script

I want to configure asterisk to execute a PHP script when the call is on the queu and another one when it leave it to a person, in another words:

I have an extension 2000 and a user “1230” who belongs to 2000, when a call come’s to 2000, asterisk must execute script1.php, but when the 1230 take down the call asterisk must execute script2.php.

with my “extension.conf” when I put the system() command before DIAL() fonction but When I put it after, it doesn’t work. so When the call come’s to 2000 I can execute the script, but when the call leaves the queu to 1230 I can’t execute the script…

[quote]exten => _912.,1,Set(recall=${FILTER(0123456789,${CALLERID(num)})})
exten => _912,2,System(php /var/www/html/scripts/script1.php ${CALLERID(num)} ${ARG2} )
exten => _912.,3,GotoIf($["${recall}" =""]?3)
exten => _912.,4,Set(chanapl=“IAX2/xpert/”)
exten => _912.,5,Macro(mcr_call,${chanapl},${EXTEN:1},30)
exten => _912.,6,hangup()[/quote]

this is the macro " mcr_call ":

[quote]exten => s,1,Dial(${ARG1}/${ARG2},30,g)
exten => s,n,Goto(s-${DIALSTATUS},1)
exten => s-ANSWER,1,System(php /var/www/html/scripts/script2.php ${CALLERID(num)} ${ARG2} )[/quote]

Thank you .