How to execute a php script after a call is hung up

Hello every one,

I am a learner in asterisk. I want to bill every call after call is hung up.
I have wrote a php script that can calculate the charge according to the CDR. But asterisk can not execute the php script after a call is hung up.Bellow is my configuration in the extensions.conf.

exten => _9xxx,1,Dial(Zap/g1/${EXTEN:0})
exten => _9xxx,2,agi,readlog.php

How can i achieve my purpose? Thanks!!!

you’ll have to call the AGI on the h exten using DeadAGI() not AGI()

IF you only want it to run on some calls, set a variable and check for that before executing it, or jump to another context before calling Dial() then exec the AGI on all calls

theres about a bakers dozen different ways to do this

Firstly,I have replace agi() by DeadAGI() but when the call is hung up asterisk still can not execute it automatically. Secondly, I add ‘include’ command to call another context [ReadCDR] to call readlog.php and the asterisk can not execute it too. extensions.conf as fowllowing:


exten => _9xxx,1,Dial(Zap/g1/${EXTEN:0})
exten => _9xxx,2,DeadAGI(),readlog.php

include => ReadCDR

exten => s,1,agi,readlog.php

h extension… its a special extension that executes on the hangup of a call…

exten => h,1,DeadAGI(script.agi)

once a call is hungup it stops executing normal extension and jumps to h so you can do post call processing

Thank you. I can read the CDR record now. But there is another problem.
My configuraiton as below:


exten => _9xxx,1,Dial(Zap/g1/${EXTEN:0})
exten => _00.,1,SetCallerID(8675510001)
exten => _00.,2,Dial(Zap/g1/${EXTEN:0})

exten => h,1,DeadAGI(readlog.php)

My readlog.php file is to read the CDR.
When I place call 9xxx and hang up and place another call 00xxxx immediately.I found that the called ID in the second call is 9xxx but not 00xxxx. It seem the asterisk have read the wrong CDR.

there is probably a problem in your AGI getting into a race condition…

there are several variables available to your AGI that you should use.

without knowing exactly what you are trying to accomplish its a bit difficult to troubleshoot something like this… as with many things theres almost always more then 1 way to accomplish anything in asterisk

I think the command 'exten => h,1,DeadAGI(readlog.php)'
read the information of the previous call not the the call that is hung up.

my readlog.php read CDR of the call that is hung up from last line of /var/log/asterisk/cdr-csv/Master.csv

why read it from there most of the data is available from the channel variables… also the cdr’s in the file are not guarenteed to be flushed as soon as you think they are

sorry,because i am not familiar with using of the variable of channel.Are there any domcument I can refer to?

which channel variable can be used?

that all depends on what information you are looking for… if you are looking for call durations etc check out “show application dial” to see what its exit variables are

it varies depending on what exactly you are looking for