Yealink XML Phonebook.xml auto-generation


#23

$results=$db->query(‘SELECT * FROM astdb WHERE key like “/cidname/%”;’);

“/cidname/%” to ‘/cidname/%’ ? - I am sorry I dont see user name besides that


(QV) #24

Hi arielgrin,

I’m looking for a way to generate phonebook to xml for grandstream. Would you be able to share your script?

Thanks!


#25

This is the one I use.

Create the following path: /var/www/html/phonebook/xml/grandstream
Create the file gs_phonebook.php in /var/www/html/phonebook with the folloiwng content

<?php
// File: gs_phonebook.php
// Description: Generating a XML Phonebook from FreePBX MySQL DB
// Modifications: Use 'users' table instead of devices allowing exclusion of some devices
// Populated both first and last name fields, Changed output filename
//!!Enable for Debug only!!
// error_reporting(E_ALL);
// ini_set("display_errors", 'ON');
// Database settings
//** Replace with your host here
$DBhost = "DB_HOST";
//** Replace with your DB user here
$DBuser = "DB_USER";
//** Replace with your password here
$DBpass = "DB_PASSWORD";
//** change only when installed Free PBX in a non-common way!
$DBdatabase = "asterisk";
// Connect to the Database and get all devices
$DBlink = mysql_connect($DBhost, $DBuser, $DBpass) or die("Could not connect to host.");
mysql_select_db($DBdatabase, $DBlink) or die("Could not find database.");
$DBquery = "SELECT extension, name FROM users ORDER BY name ASC";
$QUERYresult = mysql_query($DBquery, $DBlink) or die("Data not found.");
//Setup XMLWriter
$writer = new XMLWriter();
$writer->openURI('/var/www/html/phonebook/xml/grandstream/gs_phonebook.xml');
//** If your TFTP server is using another root directory as /tfptboot, chang the path here!
$writer->setIndent(4);
//Begin output
$writer->startDocument('1.0');
$writer->startElement('AddressBook');
//Add extensions / contacts from devices to the xml phonebook
while ($contact=mysql_fetch_array($QUERYresult)){
$writer->startElement('Contact');
$writer->writeElement('LastName',$contact['name']);
$writer->startElement('Phone');
$writer->writeElement('phonenumber',$contact['extension']);
$writer->writeElement('accountindex','0');
$writer->endElement();
$writer->endElement(); }
$writer->endElement();
$writer->endDocument();
$writer->flush();
$DBquery = "SELECT extension, name FROM users ORDER BY name ASC";
$QUERYresult = mysql_query($DBquery, $DBlink) or die("Data not found.");
//Setup XMLWriter
$writer = new XMLWriter();
$writer->openURI('/var/www/html/phonebook/xml/grandstream/phonebook.xml');
//** If your TFTP server is using another root directory as /tfptboot, chang the path here!
$writer->setIndent(4);
//Begin output
$writer->startDocument('1.0', 'UTF-8');
$writer->startElement('AddressBook');
//Add extensions / contacts from devices to the xml phonebook
while ($contact=mysql_fetch_array($QUERYresult)){
$writer->startElement('Contact');
$writer->writeElement('LastName',$contact['name']);
$writer->writeElement('FirstName');
$writer->startElement('Phone');
$writer->writeElement('phonenumber',$contact['extension']);
$writer->writeElement('accountindex','1');
$writer->endElement();
$writer->endElement(); }
$writer->endElement();
$writer->endDocument();
$writer->flush();
?>

Set ownership of path and file to asterisk:asterisk

Execute the file gs_phonebook.php with PHP to create the xml files, one is called gs_phonebook.xml for older models like GXP1200 and the other is phonebook.xml for newer models like GXP2110

Point the phones to IP_ADDRESS_OF_FREEPBX/phonebook/xml/grandstream

If you add or delete extensions to FreePBX, you need to re-execute the php file. You can create a cron job to execute the php at a predefined interval if you change extensions frequently.


(QV) #26

Thank you!