Using procedures and service programs with the PHP Toolkit for IBM i

The open source PHP Toolkit enables PHP applications to call procedures (functions) that are defined within RPG service programs on IBM i. (For more information on the toolkit, see my toolkit information page.)

Here’s an example that will work with PHP Toolkit API version 1.4.0 or higher. I also included some ‘boilerplate’ to show best practices for connecting to the toolkit and checking for a successful connection. The illustration of how to call a procedure is in the second half.

<?php
require_once('ToolkitService.php');

// connect to toolkit using DB2 credentials (can also leave blank for default authority)
try {
    $conn = ToolkitService::getInstance('*LOCAL', 'MYUSER', 'MYPASS');
} catch (Exception $e) {
    // Determine reason for failure.
    // Probably database authentication error or invalid or unreachable database.
    $code = $e->getCode();
    $msg = $e->getMessage();

    switch ($code) {
        case 8001:
            // "Authorization failure on distributed database connection attempt"
            // Usually means a wrong DB2 user or password
            echo 'Could not connect due to wrong user or password.';
            break;
        case 42705:
            echo 'Database not found. Try WRKRDBDIRE to check.';
            break; 
        default:
            echo 'Could not connect. Error: ' . $code . ' ' . $msg;
            break;
    } //(switch)
    die; // couldn't connect...handle this however you wish     
} //(try/catch)

// set stateless mode for easy testing (no 'InternalKey' needed).
// (setOptions() introduced in v1.4.0)
$conn->setOptions(array('stateless'=>true));

/* If you wish to test this script but you don't have a real service program,
 * use parseOnly and parseDebugLevel as shown below.
 * No program will be called and you'll get your original values back.
 * Simply uncomment the next line to try this great testing feature of the toolkit.
*/
//$conn->setOptions(array('parseOnly'=>true, 'parseDebugLevel'=>1));

// define several input/output params
$params[] = $conn->AddParameterChar('in', 1,'Division', 'DIV', 'A');
$params[] = $conn->AddParameterChar('in', 6,'Product', 'PROD', '123456');
$params[] = $conn->AddParameterPackDec('both', 7, 2, 'Quantity', 'QTY', '4.53');
$params[] = $conn->AddParameterZoned('out', 5, 2, 'Price', 'PRICE', '0');

// define a procedure return param. Can be any type, even a data structure
$retParam = $conn->AddParameterInt32('out', '4-byte int', 'MYRESULT', '13579');

/* Call service program procedure. 
 * In this example, assume your program is MYLIB/MYPGM and has a procedure/function 'myproc'
 * (procedure name is case-sensitive).
 * Note: specify optional procedure name in parameter 5, an array with associative index 'func'.
*/
$result = $conn->PgmCall('MYPGM', 'MYLIB', $params, $retParam, array('func'=>'myproc'));

if (!$result) {
    echo 'Error calling program. Code: ' . $conn->getErrorCode() . ' Msg: ' . $conn->getErrorMsg();
}

echo 'Called program successfully.<BR><BR>';
echo 'Input/output params: QTY: ' . $result['io_param']['QTY'] . ' PRICE: ' . $result['io_param']['PRICE'] . '<BR>'; 
echo 'Procedure return param MYRESULT: ' . $result['retvals']['MYRESULT']; 

/* 
The above will output something like:

Called program successfully.

Input/output params: QTY: 4.53 PRICE: 0.00
Procedure return param MYRESULT: 13579

*/
This entry was posted in IBM i, iSeries, PHP, System i, Zend Server, Zend's PHP toolkit for IBM i. Bookmark the permalink.

10 Responses to Using procedures and service programs with the PHP Toolkit for IBM i

  1. Steph says:

    Nice, Alan! Love the new toolkit features, as usual!

  2. Willian Serpas says:

    Hello, thnks, its a great code, so i have a situation, my pgm its on zendsvr lib, but my database its on another library, so i recieve the code RNX1216

    • Willian Serpas says:

      Hello, thnks, its a great code, so i have a situation, my pgm its on zendsvr lib, but my database its on another library, so i recieve the code RNX1216, how i can use something like I5_OPTIONS_INITLIBL=>”OTHERLIB” on ToolkitService like on i5_connect

  3. Willian Serpas says:

    Hello, thnks, its a great code, so i have a situation, my pgm its on zendsvr lib, but my database its on another library, so i recieve the code RNX1216, how i can use something like I5_OPTIONS_INITLIBL=>”OTHERLIB” on i5_connect

  4. Antonio Matteazzi says:

    Hello,
    I just started to use ToolKit to retrieve data from iSeries.
    Ii is easy to use, I call RPG program or CL Command and retrieve parameters correctly.
    Only one problem.
    When instance connection, I pass username and password, but when i try to call program, the call is execute sa QUSER and not with the correctly username of connection.
    Why ?
    Thanks for your answer.

    • Alan Seiden says:

      Antonio, in your RPG program you need to look at the “current user” field, not “user.” See the incoming data structure for “current user.”

      • Antonio Matteazzi says:

        Thank you for your answer.
        I undestand that “CURUSER” is the right field to use, but at work i have an ERP Software that work already in company and we have many programs that use USER for check the users.
        If I try to use an connection stateless = false, the field USER passed correclty but ISERIES display me all time the message CPF1240, the job XTOOLKIT ended abnormally.
        The first calling end fine, from the second error appears.

  5. However, in order to ensure that your online shopping experience is
    a safe and enjoyable one you need to exercise caution and stick to a few simple rules.

    Alternatively, they issue specific coupon codes for shoppers while checking out.
    What may be most surprising of all is that desktops
    and laptops are not dead, and are in fact responsible for more sales than smartphones.

  6. Hi Dear, are you truly visiting this web page on a regular basis,
    if so after that you will absolutely obtain nice know-how.

  7. Today, I went to the beach with my kids. I found a
    sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.”
    She placed the shell to her ear and screamed. There was a hermit crab inside and it
    pinched her ear. She never wants to go back! LoL I
    know this is totally off topic but I had to
    tell someone!

  8. Remarkable! Its genuinely amazing article, I have got much clear idea
    about from this paragraph.

  9. Its like you read my mind! You seem to know so
    much about this, like you wrote the book in it or something.
    I think that you can do with some pics to drive the message
    home a little bit, but other than that, this is great blog.
    An excellent read. I’ll definitely be back.

  10. I don’t even know how I ended up here, but I thought this post was great.

    I don’t know who you are but definitely you’re going to a famous blogger
    if you aren’t already 😉 Cheers!

  11. It’s awesome to pay a visit this web site and reading the
    views of all colleagues regarding this article,
    while I am also zealous of getting know-how.

  12. Link exchange is nothing else except it is only placing
    the other person’s webpage link on your page at suitable place and other
    person will also do similar in favor of you.

  13. Excellent article. Keep posting such kind of information on your page.
    Im really impressed by your site.
    Hi there, You have done an excellent job.

    I will certainly digg it and personally recommend to
    my friends. I’m confident they will be benefited from
    this web site.

  14. Hi, I do think this is an excellent site. I stumbledupon it 😉 I’m
    going to return once again since I book marked it.
    Money and freedom is the greatest way to change, may you be
    rich and continue to guide others.

  15. I loved as much as you will receive carried out right here.
    The sketch is tasteful, your authored subject matter
    stylish. nonetheless, you command get got an nervousness
    over that you wish be delivering the following.
    unwell unquestionably come further formerly again since exactly the same nearly a lot often inside
    case you shield this increase.

  16. Great blog here! Additionally your website a lot up fast!

    What web host are you the usage of? Can I get your affiliate hyperlink for your host?
    I want my website loaded up as fast as yours lol

  17. Your style is very unique in comparison to other people I have read stuff from.
    Many thanks for posting when you have the opportunity, Guess
    I’ll just book mark this blog.

  18. This is the perfect website for anybody who hopes to understand
    this topic. You understand a whole lot its almost tough to argue with you (not that I really will need to…HaHa).
    You certainly put a brand new spin on a subject that has been written about
    for a long time. Excellent stuff, just great!

  19. Aw, this was an exceptionally nice post. Spending some time and actual effort to generate a superb article… but
    what can I say… I procrastinate a lot and don’t manage to get anything done.

  20. Heya i am for the first time here. I came across this board
    and I find It truly useful & it helped me out a lot. I hope to give something back and
    help others like you helped me.

  21. Pretty! This was an extremely wonderful article. Thank you for providing this information.

  22. May I simply just say what a relief to discover someone who truly understands what they are talking about over the internet.
    You definitely understand how to bring an issue to light and
    make it important. A lot more people ought to check this
    out and understand this side of the story. I can’t believe you’re not more popular because you most certainly have the gift.

  23. Stripe Kits says:

    Please let me know if you’re looking for a article writer for your weblog.
    You have ome really great posts and I think I would be a good asset.
    If you ever want to take some of the lkad off, I’d love to
    write some material for your blog in exchange for a link back to mine.
    Please send me an email if interested. Regards!

  24. Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from
    you! By the way, how could we communicate?

  25. Lebron James says:

    Excellent post. I used to be checking constantly this weblog
    and I’m inspired! Extremely useful information specially the remaining phase :) I care for such info much.
    I was looking for this certain info for a long time.

    Thank you and best of luck.

  26. Lebron James says:

    Great post. I was checking constantly this weblog and I am inspired!
    Extremely useful info particularly the closing phase :
    ) I take care of such info a lot. I used to be looking for this particular info for a long time.
    Thank you and best of luck.

  27. Hi, for all time i used to check web site posts here in the early hours in the daylight,
    since i enjoy to learn more and more.

  28. Hey there just wanted to give you a quick heads up and let you know a few of
    the pictures aren’t loading correctly. I’m not sure why but I think its a linking issue.
    I’ve tried it in two different internet browsers and both
    show the same outcome.

  29. Nice blog here! Also your site loads up very fast! What web host are you using?
    Can I get your affiliate link to your host? I wish my site loaded up
    as fast as yours lol

  30. Yo me compre por uno chulo

  31. Many people would love to know how to do affiliate marketing, according
    to research by survey monkey. The Internet is a huge library of information, use
    it to learn of other opportunities and ways to increase your income.

    Keep in mind that there are thousands of others trying to make money this
    way, and only a small percentage actually experiences any amount of successful affiliate marketing.

  32. What’s up, constantly i used to check weblog posts here in the early hours in the dawn, since
    i love to learn more and more.

  33. Pretty! This has been a really wonderful post. Many thanks for providing this information.

  34. My family members every time say that I am wasting my time here
    at web, however I know I am getting familiarity all the time by
    reading such good content.

  35. Saved as a favorite, I like your blog!

  36. Hello! I simply want to give you a big thumbs up for your excellent info you have right here
    on this post. I am returning to your web site for more soon.

  37. bookmarked!!, I really like your web site!

  38. Eine hörbare Geschichte des Autors Ralf Schlatter erweckt Museumsgegenstände zum Leben.

  39. Prince says:

    alanseiden.com is very interesting, but it was hard to find your page in google, it needs more quality backlinks in order to rank.
    There is something you should read, just search in google for: Truman’s SEO Source

  40. Ridiculous quest there. What occurred after? Take care!

  41. Hi there! Someone in my Facebook group shared this website with us so I came to take a look.
    I’m definitely enjoying the information. I’m bookmarking and will be
    tweeting this to my followers! Wonderful blog and terrific design and style.

  42. TaylawhXcjr says:

    I see your site needs some fresh content, i know writing takes a lot of time, but there is solution for this hard task, just type in google:
    Mamjo’s article tool

  43. Some really excellent posts on this site, thanks for contribution.

  44. My partner and I absolutely love your blog and find almost all of your post’s to be just what I’m looking for.
    can you offer guest writers to write content available for you?
    I wouldn’t mind creating a post or elaborating on some of the subjects
    you write concerning here. Again, awesome blog!

  45. I’ve recently started a website, the info you offer on this site has helped me greatly. Thank you for all of your time & work.

  46. I am regular reader, how are you everybody? This post posted at
    this web site is genuinely fastidious.

  47. Randolph says:

    This may then get to be the first step toward alternation in your true to
    life too. Just such as your traditional resume, you must add your newest
    professions, in particular those that relate for your requirements
    field of work. Since I discovered Facebook and online community online for business,
    an abundance of new opportunities have presented themselves to me.

  48. Ming Floore says:

    Good article, thanks.

  49. Just want to say your article is as astounding. The clarity to your put up is simply nice and that i could think you’re a professional on this subject. Well along with your permission let me to seize your feed to keep up to date with forthcoming post. Thanks 1,000,000 and please continue the enjoyable work.

Leave a Reply

Your email address will not be published. Required fields are marked *