Uma consulta no banco pelo prefixo retornando uma variável com o tronco a ser usado para aquele determinado numero.
(http://www.packtpub.com/article/asterisk-gateway-interface-scripting-with-php)
Vamos supor que sua tabela de prefixo seja assim….:
id, prefixo, tronco
1, 193208, SIP/TRONCO_A
2, 193209, SIP/TRONCO_B
……..
Voce teria a seguinte agi…..
#!/usr/bin/php -q
ob_implicit_flush(false);
set_time_limit(6);
$stdin = fopen(‘php://stdin’, ‘r’);
$stdlog = fopen(‘my_agi.log’, ‘w’);
$debug = false;
/* Read input from Asterisk and output via $astOutput */
function astRead()
{
global $stdin, $debug, $stdlog;
$astOutput = str_replace("n", "", fgets($stdin, 4096));
if ($debug) fputs($stdlog, "read: $inputn");
return $astOutput ;
}
/* Write AGI command to Asterisk */
function astWrite($agiCommand)
{
global $debug, $stdlog;
if ($debug) fputs($stdlog, "write: $agiCommandn");
echo $agiCommand."n";
}
/* Handling execution input from Asterisk */
$agivar = array();
while (!feof($stdin))
{
$temp = fgets($stdin);
$temp = str_replace("n","",$temp);
$s = explode(":",$temp);
$agivar[$s[0]] = trim($s[1]);
if ($temp == "")
{
break;
}
}
/* Operational Code starts here */
$var = $argv[1];
$sql = $conn->ConsultaSQL(“Select tronco FROM tabela WHERE prefixo=’$var’ LIMIT 1”);
$res = mysql_num_rows($sql);
for($i=0; $i<$res; ++$i){ $row = mysql_fecth_array($sql); $tronco = $row[0]; } astWrite("SET VARIABLE TRONCO $tronco"); astRead(); /* Finalization of AGI script and clean-ups */ fclose ($stdin); fclose ($stdlog); exit(0); ?>
No asterisk
exten => 1,1,Agi(test.php,${EXTEN} ou ${PREFIXO}…..)
exten => 1,n,Dial(${TRONCO}/${EXTEN}…….);
……
Falta acrescentar na Agi os parametros de conexão com o banco de dados que você esta utilizando….
Creditos: Sylvio Carlos