[asterisk-users] AGI with PHP

David Klaverstyn dave at klaverstyn.com.au
Sun Dec 13 20:10:52 CST 2009


Hi All,



I'm having problems getting results from a PHP file.  This is what the CLI is showing.



    -- Executing [111 at internal:1] AGI("Console/dsp", "GoTalk.php") in new stack
    -- Launched AGI Script /var/lib/asterisk/agi-bin/GoTalk.php
[Dec 14 11:57:25] ERROR[20260]: utils.c:1019 ast_carefulwrite: write() returned error: Broken pipe

If I run the PHP file from Linux it returns the result I want but how do I get that result back into Asterisk.  I'm using Asterisk 1.6.0.10.



My extensions.conf file looks like:



exten => 111,1,Answer()

exten => 111,n,AGI(GoTalk.php)
exten => 111,n,NoOp(Result is : ${callnum})
exten => 111,n,HangUp()



My php file is:

#!/usr/bin/php
<?php

$start = '5';

$mysql_host = 'localhost';
$mysql_user = 'username';
$mysql_password = 'password';
$my_database = 'asteriskcdr';
$dbtable = 'cdr';

$from_date = '20091205000000';
$to_date = '20100104235959';

$callnum = '0';
$minutes = '0';

$query_duration = "SELECT SUM(billsec) FROM $dbtable WHERE calldate >= $from_date AND calldate <= $to_date AND disposition='ANSWERED' AND dst like '04%'";
$query_calls = "SELECT COUNT(dst) FROM $dbtable WHERE calldate >= $from_date AND calldate <= $to_date AND disposition='ANSWERED' AND dst like '04%'";
$query_cb_calls = "SELECT COUNT(dst) FROM $dbtable WHERE calldate >= $from_date AND calldate <= $to_date AND disposition='ANSWERED' AND dst like '04%' AND src like '04%'";

// Connecting, selecting database
$link = mysql_connect($mysql_host, $mysql_user, $mysql_password)
    or die('Could not connect: ' . mysql_error());

mysql_select_db($my_database) or die('Could not select database');

// Performing SQL query
$result = mysql_query($query_calls) or die('Query failed: ' . mysql_error());

// Printing results in HTML
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    foreach ($line as $col_value) {
        $callnum = $col_value;
    }
}

// Performing SQL query
$result = mysql_query($query_cb_calls) or die('Query failed: ' . mysql_error());

// Printing results in HTML
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    foreach ($line as $col_value) {
        $callnum = $callnum + $col_value;    }
}



// Performing SQL query
$result = mysql_query($query_duration) or die('Query failed: ' . mysql_error());

// Printing results in HTML
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    foreach ($line as $col_value) {
        $minutes = round($col_value/60);
    }
}

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);

echo "$callnum,$minutes";
fwrite(STDOUT,"$callnum,$minutes");
fflush($stdout);

?>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.digium.com/pipermail/asterisk-users/attachments/20091214/95975c7c/attachment.htm 


More information about the asterisk-users mailing list