Connecting to and Calling An AS400 Program with i5_toolkit

I’m starting to document some of the ways I’ve begun to use Aura’s EasyCom i5 Connect package to connect PHP to our as400. This has been a long process of trying to webify some of our programs and get the information out.  (btw, we’re a small business and yearly the package is about $600 – very, very reasonable.)

Currently, we have a webserver (Windows Server) that hosts our website. On it, we installed the required Aura software (a Windows exe GUI and a PHP extension) to connect to our as400. The PHP pages then utilize that PHP extension to make calls to our as400. On our as400 side, our programmer created small programs to access the larger programs we need. So, I pass say 3 parameters via PHP, make a call via Ericom i5, and then the RPG does the brainwork. It passes back my requested parameters and sets up a datafile I can query using basic ODBC and mySQL. We may change this in the future, but we like the logic work taking place in RPG on the as400 (where all our data resides anyway) and PHP being the fetcher of such data.

So, first up,  here’s how I connect to the as400 (after initial setup):

	/******************************************************
	*  AS400 Management
	*******************************************************/
		// connect to the iSeries / prepare for procedure call - port can be set up thru Aura GUI
		// keeping username and password in session to be able to connect to as400 as needed
		function i5_conn_AS400(){
			$conn = i5_connect('as400.mydomain.com:9999', $_SESSION['CONN_USER'], $_SESSION['CONN_PASSWORD']);

			if (!$conn) {
				$error = i5_error();
				echo " Error during connection\n";
				echo "<BR> Error number: ".$error["num"];
				echo "<BR> Error category: ".$error["cat"];
				echo "<BR> Error message: ".$error["msg"];
				echo "<BR> Error description: ".$error["desc"];
				trigger_error("I5 connection fails", E_USER_ERROR);
				exit();
			}
			return $conn;
		}

Then, here’s the preparation of parameters and call to the as400:

<?php
/* Set the Value of the Parameters */
$desc = array (
array ("name"=>"PARM1", "io"=>I5_INOUT, "type"=>I5_TYPE_CHAR, "length"=> 50),
array ("name"=>"PARM2", "io"=>I5_INOUT, "type"=> I5_TYPE_PACKED, "length"=> "8.0"),
array ("name"=>"PARM3", "io"=>I5_INOUT, "type"=>I5_TYPE_CHAR, "length"=> 1)
);

$prog = i5_program_prepare("PGMLIB/WEBLIB", $desc);

if ($prog === FALSE) {
$errorTab = i5_error();
echo "Program prepare failed <br>\n";
var_dump($errorTab);
die();
}

/* Execute Program */
$params = array ("PARM1"=>$sessID,"PARM2"=>$PARM2,"PARM3"=>$PARM3);
$retvals = array ("PARM1"=>"PARM1","PARM2"=>"PARM2","PARM3"=>"PARM3");

$ret = i5_program_call($prog, $params, $retvals) ;

if (!$ret){
print_r(i5_error());
trigger_error("i5_execute error : ".i5_errormsg(), E_USER_ERROR);
}

$ret = i5_program_close ($prog);

if (!$ret){
trigger_error("i5_program_close error : ".i5_errormsg(), E_USER_ERROR);
}
?>

I’ve actually created a connection “class” for both the Aura i5 connect and the odbc connect. That way when needed, I can simply call a function and connect to each as needed. I’m still VERY new to this all (and I just learned PHP), but figure I’d document some of this process as I go along. I have two sites currently running on this logic.