Agi php problem

please help, don’t know if it’s a bug, a version problem or…

tried several php and running them from console they run without problem, when i do it from agi they don’t do anything, last i’ve tried

#!/usr/bin/php

<?php require('phpagi.php'); error_reporting(E_ALL); ob_implicit_flush(true); set_time_limit(6); $stdout = fopen('php://stdout', 'w'); $in = fopen("php://stdin","r"); $stdlog = fopen("/var/log/asterisk/grabaciondatos.log", "a"); $debug = true; //$debug = false; $agi = new AGI(); function read() { global $in, $debug, $stdlog; $input = str_replace("\n", "", fgets($in, 4096)); if ($debug) fputs($stdlog, "read: $input\n"); return $input; } function errlog($line) { global $err; echo "VERBOSE \"$line\"\n"; } function write($line) { global $debug, $stdlog; if ($debug) fputs($stdlog, "write: $line\n"); echo $line."\n"; } while (!feof($in)) { $temp = fgets($in); $temp = str_replace("\n","",$temp); $s = explode(":",$temp); $agivar[$s[0]] = trim($s[1]); if (($temp == "") || ($temp == "\n")) { break; } } $para1 = $_SERVER["argv"][1]; $db = 'xx'; $dbuser = 'xx'; $dbpass = 'xx'; $dbhost = 'xxx.xxx.xxx.xxx'; $link = mysql_connect($dbhost, $dbuser, $dbpass) or die("Data base connection failed"); mysql_select_db($db) or die("data base open failed"); //$query ="select CodTelefono from telefonos where Numero='$para1'"; $query ="insert into grupos(Descripcion) values ($para1)"; $result = mysql_query($query) or die("Fallo la consulta"); while ($row = mysql_fetch_array($result, MYSQL_BOTH)) { // echo "VERBOSE \"$row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6], $row[7], $row[8], $row[9]\" 2\n"; $var0 = $row[0]; } //echo "VERBOSE \"Devuelvo: variables\" 2\n"; //echo "SET VARIABLE \VAR0 \"$var0\"\n"; echo "SET VARIABLE \VAR0 \-2\n"; echo "SET VARIABLE CodNombre=-2\n"; write("SET(CodLlamada)=-2"); //echo "EXEC SET VARIABLE VAR0 ".$var0; //$agi->set_variable("VAR0", "-2"); //$agi->verbose($VAR0[data]); //$agi->exec("SET VARIABLE VAR0 $var0"); //exit(); ?>

if i run
[root@elastix agi-bin]# php grabaciondatos.agi 1300
after a couple of enter keys got the row inserted (don’t know why doesn’t run all straight to the end without hitting enter key)

but if i run it from asterisk

exten => 5900,1,Answer()
exten => 5900,n,Agi(grabaciondatos.agi|1300)
exten => 5900,n,Hungup()

– Executing [5900@Grabacion:1] Answer(“SIP/1300-b7c2dcf0”, “”) in new stack
– Executing [5900@Grabacion:2] AGI(“SIP/1300-b7c2dcf0”, “grabaciondatos.agi|1300”) in new stack
– Launched AGI Script /var/lib/asterisk/agi-bin/grabaciondatos.agi
AGI Tx >> agi_request: grabaciondatos.agi
AGI Tx >> agi_channel: SIP/1300-b7c2dcf0
AGI Tx >> agi_language: en
AGI Tx >> agi_type: SIP
AGI Tx >> agi_uniqueid: 1319328373.7
AGI Tx >> agi_callerid: 1300
AGI Tx >> agi_calleridname: device
AGI Tx >> agi_callingpres: 0
AGI Tx >> agi_callingani2: 0
AGI Tx >> agi_callington: 0
AGI Tx >> agi_callingtns: 0
AGI Tx >> agi_dnid: 5900
AGI Tx >> agi_rdnis: unknown
AGI Tx >> agi_context: Grabacion
AGI Tx >> agi_extension: 5900
AGI Tx >> agi_priority: 2
AGI Tx >> agi_enhanced: 0.0
AGI Tx >> agi_accountcode:
AGI Tx >> I>
– AGI Script grabaciondatos.agi completed, returning 0
== Spawn extension (Grabacion, 5900, 3) exited non-zero on 'SIP/1300-b7c2dcf0’
and nothing happens, no matter what the php says, insert, select or just set a variable

versions are just in case

Connected to Asterisk 1.4.26.1 currently running on elastix

PHP 5.1.6 (cli) (built: Nov 29 2010 16:47:37)
Copyright © 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright © 1998-2006 Zend Technologies

thanks a lot, i already spend 2 days changing phps and trying to figure it out

What is this script supposed to do? I guess you have used some example script as a base, as I’ve seen something quite similar. I don’t really know where did you guys get this example script from, but it doesn’t seem as a good start example, as (IMO) it should either use the PHPAGI methods or not use it at all. Currently your script includes the PHPAGI class, but doesn’t use it’s methods. The PHPAGI website is at http://phpagi.sourceforge.net/ - you could find the class documentation, as well as some examples (I think).
If you could explain what this script is supposed to do I could help you rewrite it using PHPAGI methods.

thanks for your reply SGM but looks i was not clear enough oxplaining the problem.

no matter what the php code has to do when i run it from php command line it executes without problems or errors and does what is supposed to do. but when running same code from asterisk nothing happens

Sounds like you’ve got a hypothetical problem and need a hypothetical solution. I can’t really help you in this case.

it’s not “hypothetical” for sure, when i said no matter what the code has to do is because i tryied almost everything

read a line from database
write a line to database
set a variable

and all cases running it from console it works and never when dialing the extension

OK, try this script:

[code]#!/usr/bin/php -q

<? set_time_limit(30); require('phpagi.php'); error_reporting(E_ALL); $agi = new AGI(); $agi->conlog('Setting ${foo} to "bar"'); $agi->set_variable('foo', 'bar'); ?>[/code]

using a dialplan like

exten => s,1,Answer
exten => s,n,AGI(whateverscriptname.agi)
exten => s,n,Noop(Foo has a value of ${foo})

and post the console output here.

here it is

elastixCLI> agi debug
AGI Debugging Enabled
– Executing [5900@Grabacion:1] Answer(“SIP/1300-b7d00478”, “”) in new stack
– Executing [5900@Grabacion:2] AGI(“SIP/1300-b7d00478”, “grabaciondatos.agi”) in new stack
– Launched AGI Script /var/lib/asterisk/agi-bin/grabaciondatos.agi
AGI Tx >> agi_request: grabaciondatos.agi
AGI Tx >> agi_channel: SIP/1300-b7d00478
AGI Tx >> agi_language: en
AGI Tx >> agi_type: SIP
AGI Tx >> agi_uniqueid: 1319382342.4
AGI Tx >> agi_callerid: 1300
AGI Tx >> agi_calleridname: device
AGI Tx >> agi_callingpres: 0
AGI Tx >> agi_callingani2: 0
AGI Tx >> agi_callington: 0
AGI Tx >> agi_callingtns: 0
AGI Tx >> agi_dnid: 5900
AGI Tx >> agi_rdnis: unknown
AGI Tx >> agi_context: Grabacion
AGI Tx >> agi_extension: 5900
AGI Tx >> agi_priority: 2
AGI Tx >> agi_enhanced: 0.0
AGI Tx >> agi_accountcode:
AGI Tx >>
– AGI Script grabaciondatos.agi completed, returning 0
– Executing [5900@Grabacion:3] NoOp(“SIP/1300-b7d00478”, "Foo has a value of ") in new stack
– Executing [5900@Grabacion:4] Wait(“SIP/1300-b7d00478”, “1”) in new stack
– Executing [5900@Grabacion:5] Progress(“SIP/1300-b7d00478”, “”) in new stack
– Executing [5900@Grabacion:6] Playback(“SIP/1300-b7d00478”, “silence/1&cannot-complete-as-dialed&check-number-dial-again|noanswer”) in new stack
– <SIP/1300-b7d00478> Playing ‘silence/1’ (language ‘en’)
– <SIP/1300-b7d00478> Playing ‘cannot-complete-as-dialed’ (language ‘en’)
elastix
CLI>

and when i run it from php command line
[root@elastix agi-bin]# /usr/bin/php /var/lib/asterisk/agi-bin/grabaciondatos.agi

SET VARIABLE foo “bar”

[root@elastix agi-bin]#

OK, what version of Asterisk are you running? Are you running it as root or as any other user? What version of PHPAGI do you use?
Last but not least: could you please post the output of “ls -la /var/lib/asterisk/agi-bin/”?

versions are

Connected to Asterisk 1.4.26.1 currently running on elastix

PHP 5.1.6 (cli) (built: Nov 29 2010 16:47:37)
Copyright © 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright © 1998-2006 Zend Technologies

[root@elastix agi-bin]# ls -la /var/lib/asterisk/agi-bin/
total 548
drwxrwxr-x 3 asterisk asterisk 4096 oct 22 11:51 .
drwxrwxr-x 12 asterisk asterisk 4096 oct 18 08:03 …
-rwxrwxr-x 1 asterisk asterisk 43831 jul 4 2007 a2billing.php
-rwxrwxr-x 1 asterisk asterisk 1742 ago 24 2009 agi-test.agi
-rwxrwxr-- 1 asterisk asterisk 1872 oct 22 08:56 checksound.agi
-rwxrwxr-- 1 asterisk asterisk 30970 oct 22 08:56 dialparties.agi
-rwxrwxr-- 1 asterisk asterisk 13305 oct 22 08:56 directory
-rwxrwxr-- 1 asterisk asterisk 4435 oct 22 08:56 directory.agi
-rwxrwxr-- 1 asterisk asterisk 12903 oct 22 08:56 directory.lib.php
-rwxrwxr-x 1 asterisk asterisk 7240 ago 24 2009 eagi-sphinx-test
-rwxrwxr-x 1 asterisk asterisk 6276 ago 24 2009 eagi-test
-rwxrwxr-- 1 asterisk asterisk 5638 oct 22 08:56 enumlookup.agi
-rwxrwxr-x 1 asterisk asterisk 703 oct 29 2009 festival-script.pl
-rwxrwxr-x 1 asterisk asterisk 784 oct 29 2009 festival-weather-script.pl
-rwxrwxr-- 1 asterisk asterisk 1613 oct 22 08:56 fixlocalprefix
-rwxrwxr-x 1 asterisk asterisk 193 oct 23 13:05 grabaciondatos.agi
-rwxrw-rw- 1 asterisk asterisk 3064 oct 22 08:51 grabaciondatos.agi.bak
-rwxrwxr-x 1 asterisk asterisk 28770 oct 29 2009 imap.agi
-rwxrwxr-x 1 asterisk asterisk 7377 oct 29 2009 intervenir.agi
-rwxrwxr-x 1 asterisk asterisk 14530 ago 24 2009 jukebox.agi
drwxrwxr-x 7 asterisk asterisk 4096 oct 17 22:26 libs_a2billing
-rwxrwxr-- 1 asterisk asterisk 2003 oct 22 08:56 list-item-remove.php
-rwxrwxr-x 1 asterisk asterisk 3659 oct 29 2009 nv-weather.php
-rwxrwxr-- 1 asterisk asterisk 10262 oct 22 08:56 pbdirectory
-rwxrw-r-- 1 asterisk asterisk 26904 oct 18 07:20 phpagi-asmanager.php
-rwxrw-r-- 1 asterisk asterisk 65906 oct 18 07:20 phpagi.php
-rwxrw-r-- 1 asterisk asterisk 1579 oct 22 12:06 prueba.agi
-rwxrwxr-- 1 asterisk asterisk 3710 oct 22 08:56 queue_devstate.agi
-rwxrwxr-- 1 asterisk asterisk 4001 oct 18 07:09 recordingcheck
-rwxrwxr-- 1 asterisk asterisk 21117 oct 22 08:56 sql.php
-rwxrwxr-- 1 asterisk asterisk 18005 oct 22 08:56 user_login_out.agi
-rwxrwxr-x 1 asterisk asterisk 3991 oct 29 2009 wakeconfirm.agi
-rwxrwxr-x 1 asterisk asterisk 21372 oct 29 2009 wakeup.php
-rwxrwxr-x 1 asterisk asterisk 1248 oct 29 2009 weather.agi

and i’m logged as root

Try executing the script using “/var/lib/asterisk/agi-bin/grabaciondatos.agi” only. It should execute the very same way as running it using “/usr/bin/php /var/lib/asterisk/agi-bin/grabaciondatos.agi”. Also try running it without being in /var/lib/asterisk/agi-bin directory (cd /tmp; /var/lib/asterisk/agi-bin/grabaciondatos.agi).

Also, check what user asterisk is running as (ps aux | grep asterisk). I guess it’s running as asterisk, in which case you should try executing the script as the same user.