Home > Archive Post, Asterisk, PHP, PHP AGI > PHP AGI Hangup

PHP AGI Hangup

Hi all,

Hangups are handled by PHP AGI by registering a sig_handler function. Whenever a user hangs up then this function is called. However as I found out recently you cannot use the agi within the hangup handler. You can use the verbose command and see what it outputs to the system/error log as the verbose command outputs to the default php error log as well as to the Asterisk CLI. Here is some simple code to register your hangup handler:

function sig_handler($signo)
{	//Do some stuff in here
	exit(0);
}

//Register the hangup handler
if (function_exists('pcntl_signal'))
{
      pcntl_signal(SIGHUP,  "sig_handler");
}

I’ve no idea what pcntl_signal is but its installed alongside Asterisk by default I believe so you shouldn’t need to do anything extra to get it working.

If you are stuck, the approach we took is to set some variables in the dialplan and have some conditional statements within the dialplan to check these variables. There is a special extension ‘h’ which is called when a hangup occurs. The conditional Gotoif checks for a boolean statement which if true will attempt to go to the first extension, if false it will go to the second extension.

exten => h,1,GotoIf($[${EXISTS(${aVar})}]?2:4)
exten => h,2,DoSomething()
exten => h,3,GotoIf(${aVar} > 0 ? 4:5)
exten => h,4,Goto(some_extension)
exten => h,5,Goto(some_extension)

PS. I’ve noted that labels don’t appear to be working(for me at least) in the dialplan. By labels I mean:

exten => h(myLabel),2,DoSomething()

According to the docs I should be able either of below within a GotoIf:

exten => h,1,GotoIf($[${EXISTS(${aVar})}]?myLabel:4)
exten => h,1,GotoIf($[${EXISTS(${aVar})}]?2(myLabel):4)

Both of these fail for me, telling me that the extension doesn’t exist.

Categories: Archive Post, Asterisk, PHP, PHP AGI Tags:
  1. December 1st, 2009 at 05:15 | #1

    For the labels you should use :

    exten => _X.,1(begin),NoOp

    or on the hangup extension :

    exten => h,1(myLabel),DoSomething()

    Regarding the sig_handler, i didn’t quite understand what you mean by saying “you cannot use the agi within the hangup handler.”.

    Regards,

    Joao

  2. admin
    December 1st, 2009 at 06:36 | #2

    Hi Joan,

    Thanks for that info. I haven’t done Asterisk development in a while but I believe I could not previously attempt to use $agi as it is no longer present at the hangup stage. I cannot verify this at the moment however, have you found this to be the case?

  1. No trackbacks yet.

Anti-Spam Protection by WP-SpamFree