You just use popen() where ordinarily you would use fopen(). The result is a handle that you can read or write to, depending on the mode.
$content = 'hello, world';
$cmd = 'lpr';
if (!$fp = popen($cmd, 'w'))
die('Can't open pipe to printer');
fwrite($fp,$content);
fclose($fp);
If you need to add arguments to the command -- such as the name of the printer, etc -- just change $cmd accordingly.
lpr will read the input from stdin and write it to the printer queue; other parts of the CUPS suite will handle everything else.
Make sure your CUPS setup is working correctly first. From a shell, try:
echo 'hello, world' | lpr