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!


(Jameel) #27

thank you for the script. however there are some issues with it. it is not appending UTF-8 to the generated file.

and it is not working on GXP 2140 model. how to edit it and match the file format that could work for GXP 2140?


#28

You can add the lines you need, just modify the script accordingly.


(Jameel) #29

it worked. i was putting URL in grandstream phone including filename too but it had to be only directory/ and the phone automatically pulls phonebook.xml file from the directory. no need to mention file name.

it is working and bunch of thanks for the script again.


#30

Glad you got it working. Enjoy!


(Jared Busch) #31

Thanks for posting this @arielgrin. I’ve made a subfolder with a slightly reworked version that matches the direct call method from the Yealink design for now. I have a couple ideas on how to work this with and without writing files, but I don’t have a grandstream I can test with at will to finish it yet.

But even without that, you can pull out how I use the FreepBX framework instead of having to code in DB info.


(Jameel) #32

i have grandstream to test this out. does this works without accessing sql db? i am not a programmer and i am not seeing any DB credentials like the other script had?


(Jared Busch) #33

It doesn’t it need it because it uses the built in FreePBX framework that already knows how to connect.