AGI script doesn't work


exten => s,1,Noop(send sms to ${CALLERID(num)})
exten => s,n,AGI(sms.php, ${CALLERID(number)})
exten => s,n,Goto(app-announcement-9,s,1)


#!/usr/bin/php -q
$agi = new AGI();
$cid = $agi->request['agi_callerid'];
$user = "smsuser";
$pas = "smsuser";
$text = "message ";
$sms = str_replace(" ", "+", $text);
$prov = "1";
$meth = "2";
$url = "https://my-domain/goip/en/dosend.php?USERNAME=$user&PASSWORD=$pas&smsprovider=$prov&smsnum=%2B$cid&method=$meth&Memo=$sms";
if(preg_match('/^79[0-9]{9}/',$cid))	{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$out = curl_exec($ch);
	echo $out;
else	{
	echo "not mobile";

in the console I see an error message

-- Executing [[email protected]:1] Set("SIP/XXXXXXXXXXX-00000232", "__ivrreturn=0") in new stack
-- Executing [[email protected]:2] Goto("SIP/XXXXXXXXXXX-00000232", "customdests,dest-4,1") in new stack
-- Goto (customdests,dest-4,1)
-- Executing [[email protected]:1] NoOp("SIP/XXXXXXXXXXX-00000232", "Entering Custom Destination sms-sender") in new stack
-- Executing [[email protected]:2] Gosub("SIP/XXXXXXXXXXX-00000232", "sms-sender,s,1()") in new stack
-- Executing [[email protected]:1] NoOp("SIP/XXXXXXXXXXX-00000232", "send sms to XXXXXXXXXXX") in new stack
-- Executing [[email protected]:2] AGI("SIP/XXXXXXXXXXX-00000232", "sms.php, XXXXXXXXXXX") in new stack
-- Launched AGI Script /var/lib/asterisk/agi-bin/sms.php
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_request: sms.php
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_channel: SIP/XXXXXXXXXXX-00000232
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_language: ru
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_type: SIP
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_uniqueid: 1613638224.1108
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_version: 16.15.1
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_callerid: XXXXXXXXXXX
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_calleridname: XXXXXXXXXXX
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_callingpres: 0
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_callingani2: 0
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_callington: 0
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_callingtns: 0
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_rdnis: unknown
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_context: sms-sender
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_extension: s
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_priority: 2
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_enhanced: 0.0
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_accountcode:
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_threadid: 140256500864768
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_arg_1:  XXXXXXXXXXX
<SIP/XXXXXXXXXXX-00000232>AGI Rx << Could not open input file:  XXXXXXXXXXX
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> 510 Invalid or unknown command
-- <SIP/XXXXXXXXXXX-00000232>AGI Script sms.php completed, returning 0
-- Executing [[email protected]:3] Goto("SIP/XXXXXXXXXXX-00000232", "app-announcement-9,s,1") in new stack
-- Goto (app-announcement-9,s,1)

where did i go wrong?

Hello @snaggy,

Try to set the whole path to the phpagi.php file.


Thank you,

Daniel Friedman
Trixton LTD.

hello @ danielf

It does not help

I tried the same very simple script, but I get the same errors

#!/usr/bin/php -q
$agi = new AGI(); 
$cid = $agi->request['agi_callerid']; 
mail("[email protected]", 'now call:', $cid); 

Maybe the file doesn’t have the correct permissions.

Critically important that the AGI file have Unix end of line characters, if you are editing with windows, that could be it. Also must be owned by asterisk and must be executable.


Hello @snaggy,

It looks like a permission problem. Try to use this pattern in your file:
#!/usr/bin/env php
include '/etc/freepbx.conf';
include FreePBX::Config()->get('ASTAGIDIR')."/phpagi.php";
$agi = new AGI();

Thank you,

Daniel Friedman
Trixton LTD.

i created a new file in the nano editor
the script now works correctly!
thank you

So it was windows then. I use notepad++ to edit text files and always ensure to save files with UNIX EOL chars.

1 Like

For text editing, Visual Studio Code is the best free tool on the market right now.

It is cross platform, Windows, Mac, Linux.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.