Yealink XML Phonebook.xml auto-generation

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

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

Hi arielgrin,

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

Thanks!

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.

1 Like

Thank you!

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?

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

1 Like

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.

Glad you got it working. Enjoy!

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.

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?

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

Hi,

I know this is an old conversation but the content is till relevant.

I have PBXact and Fanvil X4 IP Phones. Will this script work?

if yes, can you help me with step by step command. I am a linux newbie.

I look forward to you prompt response

Hi Sorvani,

Is it possible to adapt your script for PBXact and Fanvil x4 IPPhones.

Please reply.

I am in urgent need of solution.

Regards

My script was specific for yealink. You should find out the specific format for the fanvil phone book.

Hi Arielgrin,

How can you help me out on this.

This is the link on xml operation for fanvil -

can you help me out? I am a newbie to linux and xml scripting

Thank you for your help.

UPDATE

I came across a section of the xml documentation for Fanvil that states that Fanvil is compatible with Yealink XML files.59.

My question; do you have existing yealink xml and can you help me one.

Also, PBXact seems to be blocking access / var/www/html folder. Most errors are “no permission” and file/folder not found. Can you help me out with this as well?

I am under intense pressure to deliver this project to a client.

Thank you all in advance

Fanvil phone script added to my github

You can see my full discovery process over here if you are bored.
I was lazy and did not update the comments or anything yet, so those all still reference Yealink. https://github.com/sorvani/freepbx-helper-scripts/tree/master/ContactManager_to_Fanvil_AddressBook

Happy Cake Day, Jared!
(Sorry for hijacking this thread)

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.