Michelangelo van Dam has a quick new post to his site talking about popen and pipes in command-line PHP as an alternative to the "exec" functions PHP provides to make command lines calls.

I got a question today about using commands that pipe output to other commands within PHP applications. There are two functions in PHP that are perfect for the task: popen and proc_open. But when you want to run it as a complete process, you can go about using exec, shell_exec, passthru or system and fiddle with escapeshellcmd. But often this looks messy and not reusable. A better approach would be to use "popen".

He includes a code example of how to use this method, showing a call to a command line tool and piping the results back into a PHP variable for later use. You can find out more about the use of popen in the PHP manual and accompanying examples.

On PHPMaster.com today there's a new tutorial from Timothy Boronczyk about using the proc_open function to kick off processes outside of PHP. It can be used to start up and manage (in a limited fashion) external process calls.

There are many ways we can interact with other applications from PHP and share data; there’s web services, message queuing systems, sockets, temporary files, exec(), etc. Well, today I’d like to show you one approach in particular, proc_open(). The function spawns a new command but with open file pointers which can be used to send and receive data to achieve interprocess communication (IPC).

He starts off explaining one of the fundamental concepts behind working with processes - pipes, both anonymous and named. He then moves on to the use of proc_open and the three attributes it takes - command, pipes and references for output. He includes a more practical example showing it in use - a script that converts text with wiki markup into HTML output (via this tool).

On RooJs.com there's a recent post from Alan Knowles looking at how to spam in PHP. No, nothing malicious - it's more about scalability in sending emails from PHP applications.

The reason this has been relivant in the last two weeks is two fold, first off, my slow and sometimes painfull Pman.Core and Pman.Base). It seemed like an ideal time to write some generic code that can solve both issues.

He mentions the usual method of generating numerous emails and sending them to a remote SMTP server, but points out that there's a better way. You can take advantage of queuing and batch sending techniques and, the way he decided to do it for mtrack, using queue tables and a backend runner (a cron job) that uses proc_open to send out multiple emails at once. You can see the code for it here.

While doing a lint check on a code block, a parse error was occurring on a line that contained a comment in the original source file. [...] Presumably what was happening was, even though the var_dump() call showed that actual newlines were being interpreted correctly, the r was also being interpreted rather than taken literally. This caused the comma following it to generate the error I was receiving.

An alternative to the method he was using, shell_exec, is proc_open, a function that opens a resource to handle a command execution and allows for more than just the "point and shoot" execution that things like shell_exec, or system.