Cisco XML Services Phone Directory w/Extension Status

Folks I’m trying to get the following php script working, which was originally published on the trixbox forum (RIP trixbox CE!!) by user dubya back in 2009

On my system, as far as I can tell, the value of $statusCode returned is ALWAYS ‘0’ for all extension irrespective of their actual ExtensionState. Therefore I just get a list of extensions but no meaningful ‘extension state’ as the script interprets a returned ‘0’ statusCode as ‘Idle’. Note that the status code mappings to output states (as per the case statement) I think are wrong in the script - but that can be relatively easily fixed once the correct statusCode is returned.

I’m running this on a hand installed FreePBX 2.10 with Asterisk - and as you may have guessed my PHP skills are pretty dire. Although this would have been running on an earlier version of Asterisk and FPBX, it SHOULD be possible to run this on more up to date versions. I am actually doing this with another LDAP addressbook lookup XML/php script to prove the point.

Of course to get this running you will need to have access to the XML services libraries etc from a trixbox install and copy the contents of /var/www/html/xmlservices to your FPBX server

Any trixbox veterans out there willing to have a look to get this going???


//Copyright (C) 2006 Fonality Inc. (
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//GNU General Public License for more details.

// Phone Directory Lookup Code/function added by Wendell Wilson 2007-2009

/* Asterisk Manager User/Pass */

$username = "admin";
$password = "amp111";

$server = "localhost";

/* end configuration section  */

function getExtensionStatus($extension, $name) {

        // just in case, sanitize
        $extension = intval($extension);

        global $username, $password, $server;

        $fp = fsockopen($server, 5038, $errno, $errstr, 30);
        $out =  "Action: Login\r\n";
        $out .= "UserName: $username\r\n";
        $out .= "Secret: $password\r\n\r\n";
        fwrite($fp, $out);

        $in =  "Action: ExtensionState\r\n";
        $in .= "Context: default\r\n";  // in some scenarios, this may need to be modified?
        $in .= "Exten: $extension\r\n"; // extension passed to us
        $in .= "ActionID: 1\r\n\r\n"; // I don't know what this does
        $in .= "Action: Logoff\r\n\r\n";

        $fo = "";
        while (!feof($fp)) {
                $fo .= fgets($fp, 128);

        // get the status
        $statusLine = strstr($fo, "Status:");
        // two digit code after "Status:"
        $statusCode = substr($statusLine, 8, 2);
        $statusCode = intval($statusCode); // spaces padded??

        // Is there a list of status codes?
        switch($statusCode) {

                        // return $name . " (NA Code: $statusCode)";
                        return $name . " (Not Available)";
                case 0:
                        return $name;
                case 3:
                        return $name . " (is Dialing)";
                case 4:
                        return $name . " (is Ringing)";
                case 7:
                        return $name . " (Busy)";

} // end getExtensionStatus;

require '/var/www/html/xmlservices/libs/Smarty.class.php';

$smarty = new Smarty;


require '/var/www/html/xmlservices/include/xmlservices_libs.php';
require '/var/www/html/xmlservices/include/xmlservices_config.php';
$smarty->compile_check = true;

//$smarty->debugging = true;

$query  = "SELECT name, extension FROM users ORDER BY extension";
$query .= " Limit 0,".$directoryRowCount;
$ContactList = getAllSQL($query,'asterisk');

foreach ($ContactList as &$value) {
        $value["name"] = getExtensionStatus($value["extension"], $value["name"]);


$smarty->assign('PageTitle','Local Phone Directory');