I've just been going through a couple of folders and I found a very basic PHP class I wrote to control the Music Player Daemon. Assuming the text protocol hasn't changed much it should still work fine :)

<?php
/*
 * Base MPDControl Class
 * Provide functions for connecting and retrieving raw data from MPD
 */
class MPDControl {
    var $hostname, $port, $timeout, $socket, $errno, $errstr;
    function MPDControl($hostname = 'localhost', $port = '6600', $timeout = 3) {
        $this->hostname = $hostname;
        $this->port = $port;
        $this->timeout = $timeout;
    }

    function connect() {
        $this->socket = @fsockopen($this->hostname, $this->port, $this->errno, $this->errstr, $this->timeout);
        if (!$this->socket) {
            return false;
        } else {
            $this->getData();
            return true;
        }
    }

    function disconnect() {
        $this->sendRaw('close');
        fclose($this->socket);
    }

    function getData() {
        $lines = Array();
        $line = '';
        while ((substr($line, 0, 2) != 'OK') && (substr($line, 0, 3) != 'ACK')) {
            $line = fgets($this->socket, 256);
            $lines[] = $line;
        }
        unset($lines[count($lines)-1]);
        return $lines;
    }

    function sendRaw($string) {
        fwrite($this->socket, $string."\n");
    }
}

This is then complimented with a class that extends MPDControl.

<?php
/*
 * Extended version of MPDControl
 * Provides function for getting commonly used info
 */
class ExtendedMPDControl extends MPDControl {
    function getNowPlaying() {
        $this->sendRaw('currentsong');
        $song = $this->getData();
        foreach ($song as $line) {
            $line = explode(':', $line);
            switch ($line[0]) {
            case 'Artist':
                $artist = trim($line[1]);
                break;
            case 'Title':
                $title = trim($line[1]);
                break;
            }
        }
        return Array((empty($artist) ? 'None' : $artist), (empty($title) ? 'None' : $title));
    }

    function randomOn() {
        $this->sendRaw('random 1');
        $this->getData();
    }

    function randomOff() {
        $this->sendRaw('random 0');
        $this->getData();
    }

    function play($id = '') {
        $this->sendRaw('play '.$id);
        $this->getData();
    }

    function stop() {
        $this->sendRaw('stop');
        $this->getData();
    }

    function clear() {
        $this->sendRaw('clear');
        $this->getData();
    }

    function remove($id) {
        $this->sendRaw('delete '.$id);
        $this->getData();
    }

    function add($path) {
        $this->sendRaw('add "'.$path.'"');
        $this->getData();
    }

    function getPlaylist() {
        $this->sendRaw('playlistinfo');
        $buffer = $this->getData();
        $playpos = -1;
        $artist = $album = $title = '';
        $playlist = Array();
        foreach ($buffer as $line) {
            $line = explode(':', $line);
            switch ($line[0]) {
                case 'Artist':
                    $artist = trim($line[1]);
                    break;
                case 'Album':
                    $album = trim($line[1]);
                    break;
                case 'Title':
                    $title = trim($line[1]);
                    break;
                case 'Pos': // End of song output
                    $playlist[$playpos+1] = Array($artist, $album, $title);
                    $playpos = trim($line[1]);
                    $artist = $album = $title = '';
                    break;
            }
        }
        return $playlist;
    }
}

Consider this code to be in the public domain, so have fun with it!


(Maybe) Related posts: