[Asterisk-Users] ExtensionState problems using Manager.conf API

Dax Ewbank dewbank at io2services.com
Wed Feb 2 06:45:28 MST 2005


This is my first attempt to write software of any sort.  What I am
trying to is to use a .php page to query asterisk Manager and get the
ExtensionState for each particular extension.  Then when it has the
answer it outputs an XML file for use as the directory on a Cisco 7960
phone.  What I am thinking is that when the user hits the directory
button to veiw the directory that is at this URL it will show the
directory, but based on teh extenstion status it will show whether that
person in available or not.  It should work pretty good as a blf, you
just have to request the directory again to get an updated status of
everyone that you are monitoring.

The problem I am having is that the only status that I can seem to get
from ExtensionState is -1.  No matter what I do.  I don't know why this
is happening and I have fiddled with the context how the extension is
listed, however I still jsut get -1 as a response.

It is my understanding that these are the possible status's:

-1 Channel is unavailable or does not exit
0 Channel is down and available 
1 Channel is down, but reserved 
2 Channel is off hook 
3 Digits (or equivalent) have been dialed 
4 Line is ringing 
5 Remote end is ringing 
6 Line is up 
7 Line is busy

Here is my php code that logs into the manager api and then runs through
an array of my extensions and names and returns the XML file with name
field updated with the status:

I put some comments in with //'s so that you can get an idea as to what
I was thinking here.   Most of this code I copied and pasted from
php.net and other places on asterisk lists.

<?php
header("Content-type: text/xml"); 
header("Connection: close"); 
header("Expires: -1");

print("<CiscoIPPhoneDirectory>\n");

//SET THE ARRAY WITH MY EXTENSIONS AND NAMES
$exten = array('Name1' => '201','Name2' => '202','Name3' =>
'203','Name4' => '204');

//RUN THROUGHT THE ARRAY TO GET THE STATUS OF EACH EXTENSION
foreach($exten as $name => $phone)
{
$fp = fsockopen("127.0.0.1", 5038, $errno, $errstr, 30); //OPEN THE
CONNECTION TO THE MANAGER
$out = "Action: Login\r\n";
$out .= "UserName: user\r\n";
$out .= "Secret: password\r\n\r\n";
fwrite($fp, $out);
$in = "Action: ExtensionState\r\n";
$in .= "Exten: $phone\r\n"; //HERE WHERE I AM ENTERING THE EXTENSION TO
MONITOR
$in .= "Context: intern\r\n";
$in .= "ActionID: $phone\r\n\r\n"; //I JUST USED THE EXTENSION AS THE
ACTION ID, I DON'T KNOW TO USE IT ANYWAY
$in .= "Action: Logoff\r\n\r\n";
fwrite($fp,$in);
$fo = fgets($fp, 1024);//HERE IS WHERE I GET TEH ACTUAL RESPONSE FROM
ASTERISK
$fq = strstr($fo, "Status:");//HERE IS WHERE I PARSE IT OUT THE
"STATUS:" PART
$fr = substr($fq, 8, 9);//THESE ARE THE TWO DIGITS AFTER "STATUS:"
$fs = trim($fr);//HERE I TRIM OFF THE WHITE SPACE IF THE NUMBER IS NOT
NEGATIVE
switch($fs)//HERE I RUN A SWITCH FUNCTION AND EACH CASE CORRESPONDES TO
THE VALUE FROM THE STATUS AND PRINTS THE XML ACCORDINGLY
{
case -1:
   print("\t<DirectoryEntry>\n"); 

   print("\t\t<Name>"); 
   print("$name is Unavailable"); 
   print("</Name>\n"); 

   print("\t\t<Telephone>"); 
   print("$phone"); 
   print("</Telephone>\n"); 

   print("\t</DirectoryEntry>\n"); 
   break;
case 0:
   print("\t<DirectoryEntry>\n"); 

   print("\t\t<Name>"); 
   print("$name"); 
   print("</Name>\n"); 


   print("\t\t<Telephone>"); 
   print("$phone"); 
   print("</Telephone>\n"); 

   print("\t</DirectoryEntry>\n"); 
   break;
case 1:
   print("\t<DirectoryEntry>\n"); 

   print("\t\t<Name>"); 
   print("$name is Not Available"); 
   print("</Name>\n"); 

   print("\t\t<Telephone>"); 
   print("$phone"); 
   print("</Telephone>\n"); 

   print("\t</DirectoryEntry>\n");
   break;
case 2:
   print("\t<DirectoryEntry>\n"); 

   print("\t\t<Name>"); 
   print("$name is Off Hook"); 
   print("</Name>\n"); 

   print("\t\t<Telephone>"); 
   print("$phone"); 
   print("</Telephone>\n"); 

   print("\t</DirectoryEntry>\n");
   break;

case 3:
   print("\t<DirectoryEntry>\n"); 

   print("\t\t<Name>"); 
   print("$name is Dialing"); 
   print("</Name>\n"); 

   print("\t\t<Telephone>"); 
   print("$phone"); 
   print("</Telephone>\n"); 

   print("\t</DirectoryEntry>\n");
   break;
case 4:
   print("\t<DirectoryEntry>\n"); 

   print("\t\t<Name>"); 
   print("$name is Ringing"); 
   print("</Name>\n"); 

   print("\t\t<Telephone>"); 
   print("$phone"); 
   print("</Telephone>\n"); 

   print("\t</DirectoryEntry>\n");
   break;
case 5:
   print("\t<DirectoryEntry>\n"); 

   print("\t\t<Name>"); 
   print("$name is On a Call"); 
   print("</Name>\n"); 

   print("\t\t<Telephone>"); 
   print("$phone"); 
   print("</Telephone>\n"); 

   print("\t</DirectoryEntry>\n");
   break;
case 6:
   print("\t<DirectoryEntry>\n"); 

   print("\t\t<Name>"); 
   print("$name is On a Call"); 
   print("</Name>\n"); 

   print("\t\t<Telephone>"); 
   print("$phone"); 
   print("</Telephone>\n"); 

   print("\t</DirectoryEntry>\n");
   break;
case 7:
   print("\t<DirectoryEntry>\n"); 

   print("\t\t<Name>"); 
   print("$name is Busy"); 
   print("</Name>\n"); 

   print("\t\t<Telephone>"); 
   print("$phone"); 
   print("</Telephone>\n"); 

   print("\t</DirectoryEntry>\n");
   break;
}
}
fclose($fp);
print("</CiscoIPPhoneDirectory>\n");
?>

Any input you may have would be fabulous.  please forgive my php skills,
it is everything I have learned in about one day. 

Thanks!

Dax Ewbank
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.digium.com/pipermail/asterisk-users/attachments/20050202/ac457548/attachment.htm


More information about the asterisk-users mailing list