Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

SitePoint PHP Blog:
Console Wars – PHP CLI Libraries
Jul 27, 2015 @ 09:32:35

The SitePoint PHP blog has a post that compares some of the major PHP CLI libraries, three of them at least: the Symfony console component, Hoa console and the Webmozart solution.

I have always been a big fan of console commands and I try to provide a command line interface (CLI) as much as possible in most of my PHP projects. In this article, I’ll briefly compare three PHP console command libraries.

He starts with a brief history on each of the libraries, talking about their origins and age. He then talks about the necessary dependencies each requires and the overall complexity of the code they include. Next up is some practical examples putting each to use outputting a simple message back to the user using user input for both the message and output color.

tagged: console commandline library symfony hoa webmozart cli compare

Link: http://www.sitepoint.com/console-wars-php-cli-libraries/

Michelangelo van Dam:
popen for cli commands and pipes in php
May 05, 2015 @ 10:53:38

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.

tagged: popen procopen commandline cli pipe result example

Link: http://www.dragonbe.com/2015/05/popen-for-cli-commands-and-pipes-in-php.html

SitePoint PHP Blog:
Command line PHP using Symfony Console
Dec 12, 2013 @ 10:34:15

The SitePoint blog has a new post from Daniel Gafitescu covering the use of the Symfony Console component to create command line PHP scripts quickly and easily.

As a PHP developer, you will often find yourself working with PHP in the command line. The first time I had to use it was because I would get the "Maximum execution time of 30 seconds exceeded" error on a shared server where you could not change the max_execution_time PHP setting. Nowadays building command line scripts is much easier than it used to be. If you search on Packagist you will find a lot of packages to work with the command line but the one that stands out and is the most commonly used is Symfony/Console.

He starts with what you'll need to add to your Composer configuration to pull in a development version (2.4.x-dev) of the component. With that installed, he sets up a base directory ("/app") and a basic skeleton for your application. For his first command, he creates a script that will calculate the fibonacci numbers between two given numbers. He shows how to work with the input and Output objects inside the script and the code for the finished command - including some screenshots of the output.

tagged: command line cli symfony console tutorial

Link: http://www.sitepoint.com/command-line-php-using-symfony-console/

Cal Evans:
Signaling PHP
Oct 28, 2013 @ 09:21:42

Cal Evans has a new post to his site today about a book he's published covering a topic not really focused on in the PHP world - command-line usage. The book, "Signaling PHP" covers the use of the process control extension to handle command-line signals.

Most of the PHP I write these days is CLI scripts. I really wanted to be able to trap signals in some of my scripts. I struggled with this for a while; I even spent an entire weekend googling and reading only to find out that most of the information out there was either wrong, confusing, or incomplete. I decided that once I figured it out, I was going to put everything I learned together in one place to help others that were struggling with this topic as well.

The eBook is available for purchase and download now at a suggested price of only $5 USD. If you've been looking for a quick, concise guide to using process control in PHP, you should check it out.

tagged: signaling ebook calevans cli commandline

Link: http://blog.calevans.com/2013/10/27/signaling-php/

Smashing Magazine:
Powerful Command Line Tools For Developers
Oct 30, 2012 @ 10:18:56

On the Smashing Magazine site today they've posted a list of powerful CLI tools that every developer should at least know about to help make their lives easier - six of them ranging from SSH tunnels to HTTP testing.

Good tools are invaluable in figuring out where problems lie, and can also help to prevent problems from occurring in the first place, or just help you to be more efficient in general. Command line tools are particularly useful because they lend themselves well to automation and scripting, where they can be combined and reused in all sorts of different ways. Here we cover six particularly powerful and versatile tools which can help make your life a little bit easier.

The tools they mention are all things you'd install on a unix-based system:

  • Curl
  • Ngrep (network packet searching)
  • Netcat (to work with network connections)
  • Sshuttle (SSH tunneling)
  • Siege (HTTP benchmarking)
  • Mitmproxy (capturing proxy, both HTTP and HTTPS)
tagged: commandline cli tools developer list curl ngrep netcast sshuttle siege mitmproxy

Link:

Chris Jones:
Using the PHP CLI Webserver to Identify and Test Memory Issues in PHP
Aug 15, 2012 @ 08:35:07

Chris Jones has a new post today showing how you can use PHP 5.4's built-in web server to help test for memory issues in your application (and the language).

Rasmus mentioned on IRC how he ran the [command line] tests: simply renaming the ".phpt" test files as ".php", and invoking them through the CLI webserver. The SKIPIF sections get executed, but that doesn't affect the desired outcome of testing multiple HTTP requests with different input scripts. [Here] are some quick shell scripts I put together to automate testing the OCI8 extension with valgrind.

He uses the OCI8 extension as an example, showing how to set up these scripts to enable the execution of the tests, fire up the web server and execute Valgrind to help monitor the memory of the execution.

tagged: builtin webserver cli test unittest phpt tutorial shell

Link:

Odafe Ojenikoh's Blog:
Interactive PHP CLI Applications using Zend Form
Feb 16, 2012 @ 11:09:57

Odafe Ojenikoh has submitted a post he's written up showing how to create a command line PHP application with the help of the Zend Framework and it's Zend_Form elements.

Over the weekend, I was toying with the idea of interactive cli applications using readline() and Zend Form for validating input. My motivation for using Zend Form or rather Zend Form elements was to exploit the power of features such as labels, validators, filters and the prospect of reusing forms within web application code.

His example code defines a Filter for the form errors (returning them as a string) and a "Cli" class that handles the interaction with the command line. Next up is a class defining the form elements - a prompt for a color name and either of the words "thank" and "you". The Cli class transforms the form elements into prompts on the command line and lets you define validators to check their input.

tagged: zendform zendframework cli commandline application tutorial

Link:

Chance Garcia's Blog:
MAMP PRO, PECL, SSH2, and OSX CLI (AKA acronym madness)
Nov 09, 2011 @ 11:37:34

Chance Garcia has a recent post to his blog showing how he fixed an issue with his MAMP install involving development of a SSH wrapper he developed and some testing out of PHPStorm and PHPUnit.

One thing I can say is that, even though I use a convenient app like MAMP PRO to set up my local development environment, I'm glad my sysadmin-fu is up to snuff enough to fly without the conveniences because after this ordeal, I feel like I might as well have made my MAMP stack from scratch with all the hoops I jumped tonight.

He shares a few of the things he discovered along the way like: the location of MAMP's "pecl" command, an error caused by a bad pear.conf file, doing custom compiles of PHP and libssh as a fallback and getting the extension to work in the CLI PHP version too.

tagged: osx cli mamp tutorial libssh pecl install configure compile

Link:

Gonzalo Ayuso's Blog:
Building a small microframework with PHP (Part 2). Command line interface
Aug 29, 2011 @ 13:19:11

Continuing on with his previous investigations into microframworks and what it takes to create them, Gonzalo Ayuso has posted his second part of the series - a look at working on the command line.

In my last post we spoke about building a small microframework with PHP. The main goal of this kind of framework was to be able to map urls to plain PHP classes and become those classes easily testeable with PHPUnit. Now we’re going to take a step forward. [...] It's pretty straightforward to create a command line interface (CLI) for our microframework.

He shows how to use the getopt function and the $GLOBALS superglobal to pull in arguments given to the command line script. He hooks this into the framework and makes it possible to define the controller and action to execute (with a few examples to show it in action). You can find this updated code on his github account.

tagged: microframework exercise cli commandline parse tutorial

Link:

Stuart Herbert's Blog:
Introducing phix
Mar 21, 2011 @ 08:24:05

In a new post to his blog Stuart Herbert has introduced phix, a packaging tools that can be used to manage and install different components in a framework-agnostic sort of way.

phix is a small command-line tool for PHP applications. I created it to fix (pun intended) the problem of how to easily automate the tasks involved in creating and (especially) maintaining components. These tasks aren’t built into phix; they are commands that ship with the phix distribution. You can create your own commands to run inside phix too, and it’s easy to do so

The phix tool (easily installed from PEAR) lets you either use built-in commands or custom ones to manage libraries with a few different kinds of commands - current status, initialize a library, upgrade a library and manage the full web application. As of the time of this post, the current version is phix-0.10.4.

tagged: phix library management commandline cli framework agnostic

Link: