Search code examples
asteriskagi

PHP AGI script doesn't insert into database


There is AGI-script:

#!/usr/bin/php
<?php
require_once "phpagi.php";
$agi = new AGI();
$oper_name = $argv[1];
$did_number = $argv[2];
$conn = new mysqli("localhost", "root", "passss", "mydatabase");
        $sql = "insert into  did_checking values (NOW(), '$did_number', 1, '$oper_name')";
        if ($conn->query($sql) === TRUE) {
                echo $date . ": " . "DID " . $did_number . " is alive through " . $oper_name . "\n";
        } else {
                echo $date, " Error: " . $sql . " " . $conn->error;
        }
$conn->close();
$agi->set_variable("DIDNUM", $did_number);
$agi->set_variable("OPERNAME", $oper_name);
unset ($agi);
?>

and piece of Asterisk dialplan:

[from-gsm]
exten => 8906,1,NoOp(${CALLERID(num)})
exten => 8906,n,Set(OPER_NAME='OPER1')
exten => 8906,n,Agi(did_checking.php,${OPERNAME},${CALLERID(num)})
exten => 8906,n,NoOp(DIDNUM: ${DIDNUM})
exten => 8906,n,NoOp(OPERNAME: ${OPERNAME})

As result there is no a new record in database although these lines :

$agi->set_variable("DIDNUM", $did_number);
$agi->set_variable("OPERNAME", $oper_name); 

are executed because i can see result in Asterisk console.

But when i execute script in Linux console a new record inserted.


Solution

  • You may need to explicitly COMMIT the transaction. In some cases when working through a package a transaction will be started that needs to be committed before the entry is permanent.