Debugging PHP CLI scripts on IBM i

Follow

Issue

This article tells how to debug a PHP CLI script running on an IBM i, using Studio running on your personal computer.

Environment

Zend Server 7 for IBM i, running on any supported version of IBM i.

Zend Studio 12

Another post describing how to debug a PHP CLI script running on Linux can be found here:

Debugging PHP CLI Scripts

Note

This article is written with the assumption that remote debugging is already working well for you, that you can remotely debug IBM i scripts from Studio, and that you have been able to successfully debug an IBM i script running in your browser using the Studio toolbar.  This just tells about the specific steps for PHP CLI.

Resolution

To begin, please make sure Zend Studio is up and running on your computer.  Studio needs to be active to receive the request to debug the script.

To debug the CLI script, run it in PASE. Please go to a 5250 command line and enter this command:

call qp2term

This brings up the PASE shell.

To run some command in the PASE shell, you type the command, one or more spaces, and an argument list. This is the format:

some_command some_argument another_argument 

To run a PHP CLI script on IBM i, this is the command:

/usr/local/zendsvr6/bin/php-cli 

The argument list is the script to be run. If the script takes arguments, those additional arguments also are in the argument list, but to keep things simple, let's just run a script that takes no arguments. For example, we might have a very simple script named hello.php in directory /www/zendsvr6/htdocs/support. Then the argument would be:

/www/zendsvr6/htdocs/support/hello.php 

So, to run this script using PHP CLI, we could run this command:

/usr/local/zendsvr6/bin/php-cli /www/zendsvr6/htdocs/support/hello.php 

This would run the hello.php script, and the output from the script would appear in the PASE shell. Here is how this looks in the shell:

 > /usr/local/zendsvr6/bin/php-cli /www/zendsvr6/htdocs/support/hello.php
Hello world!$

To debug the script, we need to set an environment variable before running the command. This is done at the beginning of the same line where the command gets run. To set the variable, type the variable name, an equal sign, and some string, then one or more spaces, then the command, then one or more spaces, then the argument list. This is the format:

SOME_VARIABLE='some value' some_command some_argument another_argument 

The variable we need to set to debug the CLI script is QUERY_STRING. The value for QUERY_STRING includes the ip address for your local computer running Studio, and the port Studio is using to listen for debug requests. That port will be 10137, unless you changed it in the settings. This is the format:

QUERY_STRING='start_debug=1&debug_host=<ip address>&no_remote=1&debug_port=<port>&debug_stop=1' 

The ip address for the PC used in this example is 192.168.17.4, and the port is the default 10137, so the phrase to set the QUERY_STRING environment variable is this:

QUERY_STRING='start_debug=1&debug_host=192.168.17.4&no_remote=1&debug_port=10137&debug_stop=1' 

Adding that to the command to run our simple cli script gives this:

QUERY_STRING='start_debug=1&debug_host=192.168.17.4&no_remote=1&debug_port=10137&debug_stop=1'
/usr/local/zendsvr6/bin/php-cli /www/zendsvr6/htdocs/support/hello.php

So we type all of that in to the PASE shell, and then press enter. The command moves up into the area where running commands go, but there is no output:

> QUERY_STRING='start_debug=1&debug_host=192.168.17.4&no_remote=1&debug_port=10137&debug_stop=1'
/usr/local/zendsvr6/bin/php-cli /www/zendsvr6/htdocs/support/hello.php

Now we switch over to Studio (remember, Studio has to be up and running on your computer). The script is open in Studio and is halted at the first line. Now we can set break points, run one line, run to the next break point, inspect variables, and do all the stuff we do with the debugger. After we finish debugging the script and allow it to run to completion, we return to PASE, and can see the script output there:

> QUERY_STRING='start_debug=1&debug_host=192.168.17.4&no_remote=1&debug_port=10137&debug_stop=1'
/usr/local/zendsvr6/bin/php-cli /www/zendsvr6/htdocs/support/hello.php
Hello world!$
Have more questions? Submit a request

Comments

Powered by Zendesk