Yealink XML Phonebook.xml auto-generation

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);

?>
4 Likes

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

1 Like

Here is one I wrote.

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

1 Like

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

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?

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

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 [email protected]    #
# 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:

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…

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

Buenas tardes.

En donde se pone el script

I place the script in the default location on the FreePBX server.

Google Translate: Coloco el script en la ubicación predeterminada en el servidor FreePBX.

/var/www/html

image

Gracias, y como se llama en vivo desde el teléfono, se corre el script primero para que genere algún archivo

That is correct, but the phone only refreshes every 26400 seconds

Google Translate: Eso es correcto, pero el teléfono solo se actualiza cada 26400 segundos

I grabbed the latest version of cm_to_yl_ab.php from github. Had a little hiccup though, but I tracked it down - if the actual group name in $contact_manager_group has a single quote in it, it’s not escaped properly and breaks the script. ie $contact_manager_group = “Joe’s Contacts”

never even thought about that as this wasn’t designed for open input.

Well, no telling WHAT a contact group is going to be named… I added a $_GET[‘ext’] so I could pass the extension number ( remote_phonebook.data.1.url = http://pbx.xxxx.com:81/ExtensionPB.php?ext=__line1Ext__ ). That way it pulls the private contacts for a user that they can maintain via UCP. Just name the group whatever the extension number is.

That concept is better served with the local contact function in the phone IMO. Users can save that without a web browser via the desk phone itself, or via the phone gui with the user account.

That contact list can be auto uploaded to the PBX every time it is modified.

But I see your point.

I have to disagree with you wrt the local contact function in the phone. Maintaining numbers on the phone itself is annoying at best. Let’s face it, touch screens are horrible, whether on a T48S or an iPhone X. And older users with dry skin find it particularly vexing (search on ‘zombie finger’). Giving users web access to the phone? Why do that when you already have given them UCP and taught them how to use it?

This page contains the following errors:

error on line 1 at column 15: String not started expecting ’ or "

Below is a rendering of the page up to the first error.

any thoughts?

random guess? Single quote in user name