PHP scripts can be called not only from a web server, but from CL and RPG programs as well. The concept is simple: use a program such as QSH, QP2SHELL, or QshOni, that can run PASE binaries such as PHP. With CommunityPlus+, the PHP filename to run is /QOpenSys/pkgs/bin/php.

This documentation focuses on QSH, the most reliable built-in command for calling PASE programs such as PHP. Furthermore, QSH can support multithreaded modules such as curl.

QSH CL Example

Here is a simple but complete example of calling a PHP script using QSH in a CL program:

PGM

/* Allow multithreading for multithreaded modules, such as curl */
ADDENVVAR ENVVAR(QIBM_MULTI_THREADED) VALUE(Y) REPLACE(*YES)
/* Discard output so that it doesn't create spool files */
ADDENVVAR ENVVAR(QIBM_QSH_CMD_OUTPUT) VALUE(NONE) REPLACE(*YES)
                                                     
/* Call PHP script named myscript.php with parameters */
QSH CMD('/QOpenSys/pkgs/bin/php myscript.php arg1 arg2...')

ENDPGM

Explanation of options

When using the QSH command from CL to call PASE applications such as PHP scripts, consider setting these envionmental variables:

  • QIBM_MULTI_THREADED sets up multithreading. Needed when calling PHP scripts that use the curl extension.
  • QIBM_QSH_CMD_OUTPUT redirects output so that you don’t get extra spool files or “please press enter” messages.

As mentioned above, to support multithreaded software, such as recent versions of PHP’s curl extension, add the QIBM_MULTI_THREADED environment variable, like so:

ADDENVVAR ENVVAR(QIBM_MULTI_THREADED) VALUE(Y) REPLACE(*YES)

That is, set QIBM_MULTI_THREADED to Y. By default, QSH doesn’t enable a multi-threaded environment; this variable enables it. Many PASE programs/libraries, such as curl, depend on having working threads, and without them, can fail in strange ways, such as the curl error “getaddrinfo() thread failed to start.”

You can then call a PHP script like so:

QSH CMD('/QOpenSys/pkgs/bin/php myscript.php arg1 arg2...')

Remember that paths under QOpenSys are case-sensitive.

You can redirect any output using the QIBM_QSH_CMD_OUTPUT environment variable:

ADDENVVAR ENVVAR(QIBM_QSH_CMD_OUTPUT) VALUE('file=/tmp/logfile.txt') REPLACE(*YES)

Or remove the output:

ADDENVVAR ENVVAR(QIBM_QSH_CMD_OUTPUT) VALUE(NONE) REPLACE(*YES)

QP2SHELL

QP2SHELL works for many PHP scripts, but can’t call everything that QSH can. For example, QP2SHELL is not multithreaded and does not set up proper standard I/O file descriptors needed by shell scripts. Furthermore, it requires adding a null character after parameters and can cause “Press ENTER to end terminal session” messages in RPG.

SHELL SCRIPT

Some people use a shell script to soft-code the PHP binary path or set additional variables. Here is a basic PHP shell script:

#!/bin/sh
/QOpenSys/pkgs/bin/php "$@"

If you named the above shell script callphp, you could run your PHP code and pass in several arguments using a command such as:

callphp myphp.php arg1 arg2 arg3

SSH and QP2TERM

SSH and QP2TERM establish good environments for PASE, but are generally interactive only and can’t be called easily from CL or RPG.