Running a qshell command from PHP

General discussion on Zend Core for IBM System i
Post Reply
norminnorman
Posts: 8
Joined: Mon Dec 07, 2009 7:56 pm

Running a qshell command from PHP

Post by norminnorman » Mon Dec 07, 2009 8:27 pm

Let me start out by saying while I have been programming in PHP for years, I have almost 0 experience with the as400. So please bear with me.

I am wanting to run a qshell command (which I can do) and get the output of the command (which I can't do). According to the documentation...
...
outputs - Array which describes output parameters of the command. If not provided, no output parameters are defined.
Key of the array defined i5 cl command parameter name.
"rc" is a predefined name containing the result of the command.
Value can be string. If so - it defines a php variable name to accept the parameter or array; it should have 2 elements:
1. A php variable name to accept the parameter.
2. Description of the parameter
Note: The input parameter is required if the output parameter is specified.
...
If so, I would think the following oversimplified code:

Code: Select all

$inputs = array();
$outputs = array('rc'=>'rc');
$ret = i5_command("strqsh cmd('ls')", $inputs, $outputs, $conn);
echo "$ret<br>\n";
echo "$rc<br>\n";
should print "1" because it worked and then print the output of the ls command since 'rc' is supposed to be the result from the command. Instead, I get this message:

Warning: i5_command() [function.i5-command]: Field not found in /www/zendcore/htdocs/test.php

BUT, if I leave $outputs as an empty array, I don't get the output but I get my "1", which seems to mean that it worked. What am I doing wrong? Am I going about this the wrong way? Really, instead of 'ls', I'm wanting to run something like...

Code: Select all

/QSYS.LIB/​SUBVERSION.LIB/SVN.P​GM export --non-interactive --username username --password password http://svn.server/repos/reponame/trunk/src/php/
...but if I can't get something simple like ls to work then I don't think I'll be able to get the other to work.

Any advice?

hnagao
Posts: 14
Joined: Tue Dec 01, 2009 1:19 am

Re: Running a qshell command from PHP

Post by hnagao » Thu Dec 10, 2009 3:58 am

Hi,

If you only want the output by executing 'ls' shell command, there is another way. You can get it by using 'shell_exec' command.
please try the following code.

Code: Select all

<html>
<body>
<h2>shell_exec result</h2>
<?php 
$rc = shell_exec("system 'STRQSH CMD(ls)' "); 
if (!$rc) {
print 'error occured at shell_exec !'; 
}
print $rc ;  
?>
</body>
</html>

norminnorman
Posts: 8
Joined: Mon Dec 07, 2009 7:56 pm

Re: Running a qshell command from PHP

Post by norminnorman » Fri Dec 11, 2009 6:07 pm

Thanks. I think shell_exec is the right way to go. Technically, I'm not really wanting to run 'ls' but I'm wanting to run '/QSYS.LIB/SUBVERSION.LIB/SVN.PGM'. I had tried shell_exec a while back to do this but since it didn't return anything I just assumed that it wouldn't work on the as400. Well, come to find out there was a permission problem and shell_exec only returns stdout so i couldn't see the error. So if I run

Code: Select all

echo(shell_exec('/QSYS.LIB/SUBVERSION.LIB/SVN.PGM 2>&1'));
(which sends stderror to stdout) I get "sh: /QSYS.LIB/SUBVERSION.LIB/SVN.PGM: 0403-006 Execute permission denied." So now all I have to do is figure out the permission problem and I should be good to go.

Thanks for the help!

bdietz
Posts: 12
Joined: Sun Jun 28, 2009 4:22 pm

Re: Running a qshell command from PHP

Post by bdietz » Sat Dec 12, 2009 4:33 pm

If you are going to be running a native command or program, you might be better off using one of the following:

i5_command

-or-

i5_program_prepare
i5_program_call
i5_program_close

http://files.zend.com/help/Zend-Core-i5 ... m#CL_Calls

I you really need to call it from the IFS you will probably need to create a symbolic link like:

ADDLNK OBJ('/QSYS.LIB/MYLIB.LIB/P1.PGM')
NEWLNK('/PATH/TO/PGM1') LNKTYPE(*SYMBOLIC)

Bryan

norminnorman
Posts: 8
Joined: Mon Dec 07, 2009 7:56 pm

Re: Running a qshell command from PHP

Post by norminnorman » Mon Dec 14, 2009 3:09 pm

Yeah, I'm a little confused about that. SVN.PGM is a version of the subversion client for the AS400 that (from what I understand) is compiled from AIX source code. So I'm not real sure which way I'm supposed to call it. I don't seem to be able to run it using i5_command no matter what I try. shell_exec gives me a permissions error that I don't really know how to overcome.

What is the preferred way of executing an application like this?

Post Reply