PHP and LCDProc

PHP-CLI code to show messages on an LCD with LCDProc.
Reference: http://lcdproc.sourceforge.net/docs/current-dev-html/x152.html

*WARNING*
Neither this code or my home-made USB-LCD works well with LCDProc, the results are unpredictive. It is possible to use the display without LCDProc, more about that soon.

<?php
//Code to communicate with LCDProc from PHP
//Lacks some error detection but works alright
//©Patrik Hermansson 2008

//Open a connection to LCDd
$fp = fsockopen("localhost", 13666, $errno, $errstr, 30);
//Ok?
if (!$fp) {
echo "$errstr ($errno)\n";
} else {
//The connection was opened
//Say hello to LCDd
fputs($fp, "hello");
//Get response
$output = fgets($fp, 128);
echo "hello - ".$output;

//Add a screen called 'songInfoS' (name used for future reference).
fputs($fp, "screen_add songInfoS");
//Get response
$output = fgets($fp, 128);
echo "screen_add - ".$output;

//Add a text widget called 'songInfoW' (name used for future reference).
fputs($fp, "widget_add songInfoS songInfoW string");
//Get response
$output = fgets($fp, 128);
echo "widget_add - ".$output;

//Show a message on the LCD.
fputs($fp, "widget_set songInfoS songInfoW 1 1 'Hej Patrik!'");
//Get response
$output = fgets($fp, 128);
echo "widget_set - ".$output;

//Close connection
fclose($fp);
}
?>