Informing the IBM Community

Using PHP and RPG to Communicate Together Part 2


abstract binary background

This is the second part to my article about PHP and RPG communicating together. If you missed the first part, it can be found here.

Last time we discussed PHP calling, and using, the power of RPG. This time let us switch things around and show you how easy it is for RPG, or any other programming language on our favourite box, to call a PHP script.

Example 1
In this example, our program will call a PHP script, without any parameters.
Before we continue, it is worth mentioning that the examples coming up, do not need any Apache, nor Zend, instances to be running.
This time, I have included a CL program, that performs this functionality, but could have used any program object.

Youens Feb 01

We use the QShell command to execute our PHP script called followup.php.
This script is used by our training database to send an email of the training clients that need chasing this week. A lot easier than using RPG to try to send HTML emails even with all the OS enhancements!

Example 2
And now onto the last example, our RPG program will call a PHP script passing parameters between the two.
This one is a bit different, but no more complicated thanks to the superb work by Scott Klement in allowing us to use his UNIXCMD facility. Which can be found at
My RPG program, using Scott’s examples included in his package, made life easy.
The RPG example is shown below.

Youens Feb 02

From the code we can see that php-cli will run the PHP script /php/receive_parameters.php.
In this example, we send one parameter string, and retrieve all the parameters back.
The input parameters, are delimited with colons, and then sent to the PHP.
To receive the parameters back from the PHP, we loop back through the UNIX file to retrieve all the parameters.
The corresponding called PHP script has also been included to show what was required on the PHP side.

Youens Feb 03

On the PHP side, the first thing we have to do is retrieve the parameters passed.
We use the FGETS PHP function to input the parameters to our script. As the server program used the STDIN file method, this is what we need to use.
As the RPG program passes all the parameters as a delimited string, we need to separate each element. The EXPLODE function is great for this. What a fantastic name for a function, can we have that in RPG please IBM?
Now the explode has created an array called parameters, I can loop through each of them, then, using the Echo function, return the values back to my RPG.
The spool file generated, shows the return values, which can be seen below.

Youens Feb 04

As a general recommendation, we do not put our PHP programs we are calling from the IBM i in a web facing directory, for example www/php/htdocs. I would suggest placing them anywhere else, for example /PHP off the IFS root. We can then place the appropriated security to these scripts knowing they cannot be accessed via Apache.
If you need a more thorough explanation of Scott’s UNIX command, visit his website.
If you have any problems with the toolkit, switch on the debugging feature and check the log, which by default is /usr/local/zendsvr6/var/log/tkit_debug.log, and don’t forget the PHP logs, they are your friend, use them.
Hopefully, this article will show you how easy it is to bring PHP into our applications. PHP as a programming language is excellent in performing functions, that traditionally RPG program has had difficulty in processing, for example calling webservices, emails, writing to Excel etc.
Zend has done a fantastic job in providing an excellent PHP server stack on the IBM i, let us all embrace this and get on the PHP bandwagon.
Andy Youens is an IBM i consultant/instructor at Milton Keynes, UK-based FormaServe Systems.


How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.


2 responses to “Using PHP and RPG to Communicate Together Part 2”

  1. Question: you stated that Apache and Zend do not need to be running; does that mean that Zend Server does not need to be installed? Is it possible to run PHP on iSeries without Zend Server?

  2. Katrina McKinney avatar
    Katrina McKinney

    I have PHP that is successfully calling an RPGLE program to send an email.
    I wanted to added something to the RPGLE program and when called directly from the IBM, the program acts as expected. However, when the PHP calls the program, it seems that it’s not calling the newest version of the RPG…

    Any suggestions on how to debug this issue or figure out why the PHP is not calling the “newest” version of the RPG program?