Parsing ip and calledid with AGI

Get help with installing, upgrading and running Asterisk.

Moderators: muppetmaster, Moderator, Support

Parsing ip and calledid with AGI

Postby ales187 » Wed Mar 19, 2008 2:23 am

Hello,

I'm using PHPAGI with Asterisk and my Aastra.

My problems is: I'm trying to parse the ip of the phone who is making a call, and called id, which the phone called.

Example:

exten => _91.,1,AGI(my_ip.php)

So whenever my phone calles 91xxx, the AGI script will start - Parse the IP of my phone and the number I called (example: 91555), and put it in variables, so i can use it in my XML script for Aastra.

I tried PHPAGI my_ip.php:


#!/usr/bin/php -q
<?php
set_time_limit(30);
require('phpagi.php');

function my_ip(&$agi, $peer)
{
$ip = 'unknown';
$asm = $agi->new_AsteriskManager();
if($asm->connect())
{
$peer = $asm->command("sip show peer $peer");
$asm->disconnect();

if(!strpos($peer['data'], ':'))
echo $peer['data'];
else
{
$data = array();
foreach(explode("\n", $peer['data']) as $line)
{
$a = strpos('z'.$line, ':') - 1;
if($a >= 0) $data[trim(substr($line, 0, $a))] = trim(substr($line, $a + 1));
}
}

if(isset($data['Addr->IP']))
{
$ip = explode(' ', trim($data['Addr->IP']));
$ip = $ip[0];
}
}
$agi->text2wav("Your IP address is $ip");
}
?>


And I get this error:


-- Launched AGI Script /var/lib/asterisk/agi-bin/my_ip.php
AGI Tx >> agi_request: proba.php
AGI Tx >> agi_channel: SIP/800-08cf9f78
AGI Tx >> agi_language: en
AGI Tx >> agi_type: SIP
AGI Tx >> agi_uniqueid: 1205828466.54
AGI Tx >> agi_callerid: 800
AGI Tx >> agi_calleridname: 800
AGI Tx >> agi_callingpres: 0
AGI Tx >> agi_callingani2: 0
AGI Tx >> agi_callington: 0
AGI Tx >> agi_callingtns: 0
AGI Tx >> agi_dnid: 706
AGI Tx >> agi_rdnis: unknown
AGI Tx >> agi_context: office
AGI Tx >> agi_extension: 706
AGI Tx >> agi_priority: 2
AGI Tx >> agi_enhanced: 0.0
AGI Tx >> agi_accountcode:
AGI Tx >> LI>
AGI Rx << I>
AGI Tx >> 510 Invalid or unknown command
-- AGI Script proba.php completed, returning 0



I think it stopes at: function my_ip(&$agi, $peer)

I have no idea why.

Could somebody help me?[/i]
ales187
Newsterisk
 
Posts: 22
Joined: Wed Feb 06, 2008 2:43 am

Postby ales187 » Wed Mar 19, 2008 7:49 am

I've got it:

#!/usr/bin/php -q
<?php
require 'phpagi.php';
$agi = new AGI();


$ip = 'unknown';
$asm = $agi->new_AsteriskManager();
if($asm->connect())
{
$stev = $agi->request["agi_callerid"];
$peer = $asm->command("sip show peer $stev");
$asm->disconnect();

if(!strpos($peer['data'], ':'))
echo $peer['data'];
else
{
$data = array();
foreach(explode("\n", $peer['data']) as $line)
{
$a = strpos('z'.$line, ':') - 1;
if($a >= 0) $data[trim(substr($line, 0, $a))] = trim(substr($line, $a + 1));
}
}

if(isset($data['Addr->IP']))
{
$ip = explode(' ', trim($data['Addr->IP']));
$ip = $ip[0];
}
if(isset($data['Def. Username']))
{
$ext = explode(' ', trim($data['Def. Username']));
$ext = $ext[0];
}

}

$sperma = $agi->request["agi_dnid"];

$agi->text2wav("Your IP address is $ip and your number is $ext and you called $sperma");


?>


Works perfectly. Thank me for all the help :)
ales187
Newsterisk
 
Posts: 22
Joined: Wed Feb 06, 2008 2:43 am


Return to Asterisk Support

Who is online

Users browsing this forum: Google [Bot] and 1 guest