In this article, I wish to take you through how we can communicate between PHP and RPG on our IBM i.
I recently had a request from one of our clients asking if I had any examples of calling an RPG program from a PHP script. This gave me the idea, via the PowerWire platform, to explain how we can get these two fantastic languages working together.
As you may know, PHP is an excellent programming language that coupled with our traditional programs, can provide functionality, that by default, can be difficult and at times frustrating to provide. For example, try sending an HTML email from RPG, or get RPG to use a webservice, umm….
PHP is an ideal programming language to complement our conventional programming languages, not replace them.
The majority of the examples in this article, will use the excellent XML Services, also known as the IBM i Toolkit developed by IBM and Zend. The methods and functions included in the toolkit, really ease our development.
In the first example, we will show how we can call a RPG program, (can be any IBM i program object in fact) without passing any parameters.
To explain, firstly, we make a connection to our IBM i, using the XML Toolkit. This is achieved by the getInstance method of the Toolkit class.
The variable $conn is used as the Toolkit object and used to perform the methods of the toolkit.
Next we use the PgmCall method, passing the name of the program, the library it resides in, and thirdly, the parameter variable, which is null as we are not passing any variables into the program POWERWIRE/FSS1. We could have omitted the last three parameters on our call as these are optional.
If we need to know if the call was successful we can check if there is any value in the parameter $result, returned from the program call.
How easy was that! All due to the XML Toolkit, that Zend and IBM provided.
Let us move on now and show how to pass parameters to our programs from a PHP script.
In this example, a call to our program will change the value of the User Profile parameter, so we can review it after the call. This is contained in the array $outputParams[‘USER’].
The Toolkit will help us again with this. We have to create an array of the attributes of the parameters, type, size, etc. This can be seen using the array variable $param.
As in the previous example, we use the PgmCall method to run our program call FSS3 in library POWERWIRE.
Please note that in the above example, to save space I have not included the initial connection to our server, refer back to the first example, if you need to know how to do this.
Now we have that working as we intended, let’s get a bit more complicated. This time we are going to pass a data structure for our RPG to receive.
In this example, we are passing four parameters to our RPG program. Two character strings, an integer and a data structure.
The data-structure is built, using an array, into the variable $ds, then added to the parameter list. Obviously, our RPG at the other end, must be expecting a data-structure.
All easier than you may have been expecting.
In the next article, we will be showing how simple it is to integrate PHP into our applications, by demonstrating how we can execute a PHP script and pass parameters between the two from an RPG program.
Andy Youens is an IBM i consultant/instructor at Milton Keynes, UK-based FormaServe Systems.