ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

PHP with CGI

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP with CGI

    Hi,

    I am using V5R4.

    1) I have a CGI web application that works find and dandy (RPG ILE program responds to browser request and Apache server returns the generated HTML+ Javascript to client browser).

    2) I have Zend Core for i5/OS installed on the same machine and can I happily have the Apache server parse PHP content, before returning response to client browser.

    What I cannot accomplish, yet, is a configuration that will allow parsing of PHP statements *within* the content returned by the CGI program.

    Anybody done this?

    Anybody interested in doing this?


    Regards,

    Simon
    Last edited by gcraill; September 5, 2007, 12:44 AM. Reason: page overflow error

  • #2
    Re: PHP with CGI

    The issue is you are referring to your RPG CGI program by some extension other than .php. The easiest (and I think most secure way) is to use the AddType directive in your Apache configuration and have all .html file processed by PHP. Check out:http://http://www.webmasterworld.com/forum10/5296.htm

    Comment


    • #3
      Re: PHP with CGI

      Originally posted by simonuk View Post
      1) I have a CGI web application that works find and dandy (RPG ILE program responds to browser request and Apache server returns the generated HTML+ Javascript to client browser).

      What I cannot accomplish, yet, is a configuration that will allow parsing of PHP statements *within* the content returned by the CGI program.
      what are you using to interface RPG to CGI? CGIDEV2, xTools...websmart...

      there may be some quirk with the interface and not your configuration of the webserver.
      I'm not anti-social, I just don't like people -Tommy Holden

      Comment


      • #4
        Re: PHP with CGI

        Hi Tom,

        FYI - I am using QtmhRdStin and QtmhWrStout in RPG to read from and write via the HTTP server.


        Actually I have now found a solution to my problem.

        Instead of my HTML page in the browser posting to the HTTP server and having that invoke a CGI program...my solution is as follows:

        The HTML page in the browser posts to a .PHP script (via the ZENDCORE (native) Apache Instance)...it is this PHP script ithat nvokes* the CGI program (which returns an HTML page with embedded PHP)...the .PHP script then parses any embedded PHP using an EVAL operation.

        *CGI program is invoked with FPUTS operations after a socket is opened (FSOCKOPEN).

        Hey Presto! - My CGI application now supports embedded PHP.

        Cheers,

        Simon

        Comment


        • #5
          Re: PHP with CGI

          Cool Simon...If possible post some code snippets so others can "follow" where you have been.


          jamie
          All my answers were extracted from the "Big Dummy's Guide to the As400"
          and I take no responsibility for any of them.

          www.code400.com

          Comment


          • #6
            Re: PHP with CGI

            Hi Jamie,

            I am writing up the solution for my internal docs...I will try and post some info on this thread when I am done.

            Cheers,

            Simon

            Comment


            • #7
              Re: PHP with CGI

              Hi all,

              So...here's a solution for parsing/excecuting PHP embedded in the response from a CGI program.

              To recap...


              ***Scenario:

              A CGI (RPG) program is sitting on the System i, that responds to POST requests from a browser (via Apache HTTP server) and sends a HTML page as a response.


              *** Problem:

              Sadly any PHP code in the HTML page sent from the CGI program to the browser is never parsed/executed.


              *** Solution:

              Have the HTML page in the browser invoke an intermediate PHP script.

              The PHP script sends the POSTed data to the CGI program...and receives the CGI program's response.

              When the CGI program sends the response back...the PHP script uses the EVAL statement to parse/execute any embedded PHP and returns the results to the browser.

              Hurrah!


              Code:
              <?php
              // ** CGIPHP.PHP **
              // Author: Simon Cockayne
              // Date: 2007/December/27
              // NOTE: Based on an initial script provided by Zend Support (Helpdesk ticket ID 173222)
              // NOTE: SendToHost based on http://www.faqts.com/knowledge_base/view.phtml/aid/12039
              // *** Maintenance: NONE
              
              
              // Determine posted data
              if (count($_POST) > 0) {
              	foreach ($_POST as $key => $value) {
              		if (isset($postdata)) $postdata .= "&";
              		$postdata .= $key."=".$value;
              	}	
              } else {
              	$postdata = "";
              }
              
              // Convert posted data from ISO8859 to IBM-037
              $postdata = iconv("ISO8859-1", "IBM-037", $postdata);
              
              // Send posted data to CGI Program ( via Apache Server instance)
              $buf = (sendToHost("127.0.0.1", "4201", "POST", "/CGIPGM", $postdata  ));
              
              
              $stripped = explode('<html lang="en-US">', $buf, 2);
              
              $stripped = explode("</HTML>", $stripped[1], 2);
              
              
              $data = "?><html>" . $stripped[0] . "</html>";
              
              // Parse and execute embedded PHP
              eval($data);
              
              
              /* sendToHost 
              *
              * Params:
              * $host - hostname
              * $method - get or post, case-insensitive
              * $path - The /path/to/file.html part
              * $data - The query string, without initial question mark
              * $useragent - If true, 'MSIE' will be sent as
              the User-Agent (optional)
              *
              */
              
              function sendToHost($host,$port,$method,$path,$data,$useragent=0)
              {
              
              // Supply a default method of GET if the one passed was empty
              if (empty($method)) {
              $method = 'GET';
              }
              $method = strtoupper($method);
              
              // Open a socket to the CGI PGM (via Apache Server)
              
              $fp = fsockopen($host, $port);
              
              // Format data if GET
              if ($method == 'GET') {
              $path .= '?' . $data;
              }
              
              // Populate the HTTPRequest header
              fputs($fp, "$method $path HTTP/1.1\r\n");
              fputs($fp, "Host: $host\r\n");
              fputs($fp,"Content-type: application/x-www-form- urlencoded\r\n");
              fputs($fp, "Content-length: " . strlen($data) . "\r\n");
              if ($useragent) {
              fputs($fp, "User-Agent: MSIE\r\n");
              }
              fputs($fp, "Connection: close\r\n\r\n");
              
              // Send the posted data if POST
              if ($method == 'POST') {
              fputs($fp, $data);
              }
              
              // Clear the response buffer
              $buf = "";
              
              // Populate the response buffer  
              while (!feof($fp)) {
              $buf .= fgets($fp,128);
              }
              
              // Close the socket
              fclose($fp);
              
              // Return the response buffer
              return $buf;
              }
              ?>

              Happy New Year!

              Simon Cockayne
              Last edited by simonuk; December 31, 2007, 09:50 AM.

              Comment


              • #8
                Re: PHP with CGI

                Thanks for sharing! Nice and easy to follow...Happy New Year.
                All my answers were extracted from the "Big Dummy's Guide to the As400"
                and I take no responsibility for any of them.

                www.code400.com

                Comment

                Working...
                X