FreePBX | Register | Issues | Wiki | Portal | Support

Yealink XML Phonebook.xml auto-generation


(Greg) #1

So, I wrote a script that generates the XML Phonebook for Yealink phones.

Enjoy!

Note: username and password need to have select access to the database

<?php
$mysql_conn = mysql_connect('localhost','username','password');
mysql_select_db('asterisk', $mysql_conn);
$query = 'SELECT description AS Name, user AS Telephone FROM devices WHERE 
tech="sip"';

$result = mysql_query($query, $mysql_conn);
if (! $result){
echo mysql_error();
}

$Output = "<YealinkIPPhoneDirectory>\n";
while($data = mysql_fetch_assoc($result)){
    $results[] = $data;
    $Output .= " <DirectoryEntry>\n";
    foreach($data as $key => $value){
            $Output .= "  <$key>$value</$key>\n";
    }
    $Output .= " </DirectoryEntry>\n";
}
$Output .= "</YealinkIPPhoneDirectory>\n";
// echo $Output;

file_put_contents('Phone.xml',$Output);

?>

#2

Great script! I have a similar one that generates phonebook simultaneously for yealink, grandstream and snom.


(Jared Busch) #3

Here is one I wrote.

This one is designed to be called live from the phone button.


Yealink phones and PBXact Contacts
(Jared Busch) #4

Apparently, I cannot edit the post any more, but I cleaned up the repository, the address changed a bit.


(Tom Ray) #6

So I have a question about these scripts. Is there a reason they only look for local extensions on the PBX vs the actual contact lists? I mean the scripts are basically kicking out an internal directory but what if people what other contacts in their phonebook directory?


(Jared Busch) #7

@BlazeStudios I have another one that in there that looks at the Contact Manager.


(Comdif) #9

To use freepbx phone book I have make this very simple code working like a charm
Use this page as remote phone book, name is as you like and put it in /var/www/html dir.

<?php
################################################
#    COMDIF INNOVATION Christian-Zeler@2019    #
# Dynamic XML from Free-PBX Asterisk Phonebook #
################################################
header("Content-Type: text/xml");
// Connect Asterisk Sqlite database
$db = new SQLite3('/var/lib/asterisk/astdb.sqlite3');
// Search cidname datas
$results=$db->query('SELECT * FROM astdb WHERE key like "/cidname/%";');
// make header for XML format
echo'<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n';
// XML doc start
echo'<contactIPPhoneDirectory>\n';
$index = 0;
// Loop to print Directory 
while ($row = $results->fetchArray())
	{
    $num = explode('/',$row[0]);
	$index= $index + 1;
	echo "    <DirectoryEntry>\n";
	echo "        <Name>" . $row[1] . "</Name>\n";
	echo "        <Telephone>" . $num[2] . "</Telephone>\n";
	echo "    </DirectoryEntry>\n";
	}
// End XML doc
echo "</contactIPPhoneDirectory>\n";
// That's all folks :-)
?>

Enjoy :slight_smile:


(Greg) #10

I’m not a php expert (although I have 40+ years programming in other languages…) what is the purpose of the $index variable in that script? Seems to not do anything…


(Jared Busch) #11

Not true it increments every loop. So that is something… He’s not using it to do anything, but it does increment.