Automatic Call with Asterisk Manager API, PHP CLI and Mysql

Hi All,

I hope you could help/suggest a solution about my problem. I have a php cli script below which pull numbers from mysql, it dials the numbers using manager api and when someone answered, it will play ivr.
My problem is, every time it loops to dial a number it always logs-on to AMI then log off.

How do I make it that it only log on once to AMI and only logoffs when all numbers from the table are dialed?

Comments/suggestions are very much appreciated.

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

<?php $strUser = 'astmanuser'; $strSecret = 'secret'; $con = mysql_connect('localhost','user','pass'); if (!$con) { die('Could not connect to server!'); } $db = mysql_select_db('asterisk', $con); if (!$db) { die ('Database error!'); } $ctr = 0; $i=0; $numberlist = array(); $nlist = array(); $sqlquery = "SELECT PHONE FROM NUMBERPOOL WHERE STATUS = 1 LIMIT 10"; $result = mysql_query($sqlquery); $numbercount = mysql_num_rows($result); while ($row = mysql_fetch_row($result)) { $nlist[$i] = $row[0]; $i++; } $numberlist = $nlist; $oSocket = fsockopen ('localhost', 5038, $errno, $errstr, 20); if (!$oSocket) { die("$errstr ($errno)"); } while($ctr<$numbercount){ $number = $numberlist[$ctr]; $strCallerId = "Test Call <$number>"; fputs($oSocket, "Action: login\r\n"); fputs($oSocket, "Events: off\r\n"); fputs($oSocket, "Username: $strUser\r\n"); fputs($oSocket, "Secret: $strSecret\r\n\r\n"); while (!feof($oSocket)){ if (preg_match("/Message: Authentication accepted/i", fgets($oSocket, 128))) { fputs($oSocket, "Action: originate\r\n"); fputs($oSocket, "Channel: SIP/$number"."@siprovider\r\n"); fputs($oSocket, "WaitTime: 30\r\n"); fputs($oSocket, "CallerId: $strCallerId\r\n"); fputs($oSocket, "Exten: s\r\n"); fputs($oSocket, "Context: play-ivr\r\n"); fputs($oSocket, "Priority: 1\r\n\r\n"); fputs($oSocket, "Action: Logoff\r\n\r\n"); break; } } $sqlquery = "update numberpool set status = 2 where phone = '$number'"; mysql_query($sqlquery); if($ctr >=($numbercount-1)){ $sqlquery = "SELECT PHONE FROM NUMBERPOOL WHERE STATUS = 1 LIMIT 10"; $result = mysql_query($sqlquery); $numbercount = mysql_num_rows($result); while ($row = mysql_fetch_row($result)) { $nlist[$i] = $row[0]; $i++; } $numberlist = $nlist; $ctr = 0; } else{ $ctr++; } } fclose($oSocket); mysql_close($con); ?>[/code][/code]

Put your login / logoff commands outside the loop? :wink:

davevg, thanks for the quick reply.

I can put the login/logoff outside the loop but it will only dial the next number if someone answered/channel hangup.
I forgot to state on my post what I’m trying to achieve. I would like the script to dial the numbers without waiting for the current number to be answered or hangup.

If I put the login/logoff inside the loop It will dial the numbers continuously but that I don’t want because it will always logon/logoff.

Add:

Async: True

to the Action: Originate section. It will make it so it doesn’t wait for a response before it goes on to the next one.

Great! Thanks so much for your big help dave. :smiley: