PHP scripts can be called not only from a web server, but from a command line and CL and RPG programs. You will need to call a program such as QSH that can run PHP and other PASE binaries.

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. Unlike QP2SHELL, QSH can support multithreaded modules such as curl. (While we focus on QSH here, you can find additional options with the free QshOni tool.)

QSH CL Example

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

PGM

/* OPTIONAL: If your script uses curl, add to allow multithreading. */
ADDENVVAR ENVVAR(QIBM_MULTI_THREADED) VALUE(Y) REPLACE(*YES)
/* OPTIONAL: If you don't want output to create spool files, add to discard output */
ADDENVVAR ENVVAR(QIBM_QSH_CMD_OUTPUT) VALUE(NONE) REPLACE(*YES)
                                                     
/* Specify PHP binary, PHP script name, and any arguments */
QSH CMD('/QOpenSys/pkgs/bin/php myscriptname.php arg1 arg2...')

/* OR: If you have a site-specific custom PHP configuration, specify INI locations this way */ 
/* QSH CMD('PHPRC=/www/seidenphp/phpconf +
            PHP_INI_SCAN_DIR=/www/seidenphp/phpconf/conf.d/ +
            /QOpenSys/pkgs/bin/php myscript.php arg1 arg2...') */

ENDPGM

Note: Because QOpenSys is a case-sensitive file system, MYSCRIPTNAME.PHP will not match myscriptname.php. The old SEU/PDM editor makes everything uppercase by default. Users of SEU should see these instructions on enabling mixed case in SEU.

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.

Got Issues? GET ANSWERS!

Your open source should be trouble-free. If your current open source environment is anything less than perfect, we can help!

(or call 201.447.2437)

Seiden Group Documentation Library

Open Source Environment
ODBC
Apache & Other Web Servers
CommunityPlus+ PHP Setup
NOTE: To gain access to any protected pages, you must first download CommunityPlus+.
PHP (General)
Node.js
Python
QSHONI
VS Code for IBM i