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.
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.
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!
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 https://www.scottklement.com/unixcmd/
My RPG program, using Scott’s examples included in his package, made life easy.
The RPG example is shown below.
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.
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.
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.