Using D-Bus with PHP

The D-Bus system used on Linux is usually accessed through Python. However, D-Bus can also be used with PHP through the PECL extension "dbus." In the following article, I briefly describe how to control the Tomboy note-taking program with PHP and read all its notes.

Installing DBus for PHP

To get started, the PHP module must first be installed through the PECL installer. I use the current Ubuntu Linux 10.04 with PHP installed. On Ubuntu, the PECL installer should already be preinstalled. Otherwise, it can be installed along with PEAR.

On the console, running pecl install dbus is enough for it to download the C source files and configure and compile the PHP module through phpize. Once everything has completed, a new shared object file, dbus.so, has been created. On Windows, probably dbus.dll.

This must be registered in the php.ini file. Simply enter the following lines in php.ini (preferably in the section intended for this in php.ini):

extension = dbus.so

Since I use php-cli, no web server configuration needs to be reloaded, so that completes the installation of DBus for PHP.

The example program

<?php
if (!extension_loaded('dbus')) {
    die('Extension dbus is not loaded');
}

$dbus = new Dbus(Dbus::BUS_SESSION, true);
$tomboy = $dbus->createProxy(
            'org.gnome.Tomboy',
            '/org/gnome/Tomboy/RemoteControl',
            'org.gnome.Tomboy.RemoteControl'
        );  

$noteTitles = $tomboy->ListAllNotes(); // gibt ein DBusArray Objekt zurück
foreach ($noteTitles->getData() as $noteUri) {
    //echo $noteTitle = $tomboy->GetNoteTitle($noteUri); // Ausgeben des Titels
    echo $tomboy->GetNoteContents($noteUri); // Ausgeben der kompletten Notitz
}

Anyone who wants to play around with DBus and Tomboy themselves can find a description of the Tomboy API in an article by Ryan Paul. The code there is in Python, though. But mapping the methods can certainly be done in a short time.

A little tip for writing your own code: desktop notifications are also very easy to integrate into your own application.

Have fun trying it out.