Why Do Cool Kids Chose PHP to Build Websites?

PHP Developer's Journal

Subscribe to PHP Developer's Journal: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get PHP Developer's Journal: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


PHP Authors: Hovhannes Avoyan, AppDynamics Blog, Trevor Parsons, Andreas Grabner, Harald Zeitlhofer

Related Topics: Apache Web Server Journal, Java Developer Magazine, PHP Developer's Journal

Apache Web Server: Article

Intro to LXP, the XML/PostgreSQL Application Server

LXP is not a cure-all or replacement for Perl, Python, or PHP, but is spiffy when used in the right situation.

(LinuxWorld) -- This is a technical overview of the features and use of the LXP Application Server, which was developed by Command Prompt, Inc. Before we begin, let me share a brief disclaimer: I work for Command Prompt. I don't think LXP is a cure-all or replacement for Perl, Python, or PHP, but find it invaluable when used in the right situation.

LXP (also known as mod_lxp) is an application server that runs as an Apache DSO (Dynamic Shared Object) module. It is designed to allow for the simplified development of data-driven, dynamic Web sites involving support for XML, and SQL (through PostgreSQL).

The use of PostgreSQL with LXP is not required, but it is currently the only database LXP supports. Oracle is to be supported soon. LXP is designed to allow even an entry-level developer to quickly provide a useful and marketable product for a customer or department. LXP does this by using features similar to other application servers, such as:

  • Simple markup structure
  • Ad-Hoc SQL Statements
  • XML Support
  • Data Parsing
  • Direct Apache Sub-Requests

LXP, like PHP, is embedded into the HTML page you are developing, and is parsed as Apache serves the page to the browsing client. Here is a simple example of a PHP script:

<?
 if ($hello == "1") {
   print ("Hello World");
} else {
   print ("It is not true");
}
?>

The following is LXP markup providing the same functionality of the above example:

<lxp>
 <if hello="1">
    Hello World!
 </if>
 <else>
    It is not true
 </else>
</lxp>

Although the above example does not show the true flexibility of LXP, it does provide a brief introduction into LXP markup, and some basic logic. The above example also provides an insight to why LXP was created. LXP is designed to be simple. It is designed so that anyone who can write basic markup (such as HTML) can provide dynamic data to the Web.

Alternative languages such as PHP are designed for developers. They are powerful and have their uses. Anyone who develops in C, C++, Perl, Java, or many other languages could easily become competent with PHP in a Sunday afternoon. LXP, on the other hand, is designed to give more power back to the technical user without limiting developers who choose to use it.

For example, if you wanted to create a connection to a PostgreSQL database and then return the results to a Web browser using PHP, you might code something like this:

<?
  $conn = pg_connect("host=localhost port=5432 dbname=somedb user=someuser");
  if (!$conn) {
    print("Connection Failed.\n");
    exit;
  }
  
  $result = pg_exec($conn, "SELECT name,c_city
                            FROM company_auth");
  for ($lt = 0; $lt < pg_numrows($result); $lt++) {
    $name = pg_result($result, $lt, 0);
    $city = pg_result($result, $lt, 1);
    
    print("Name: $name<BR />\n");
    print("City: $city<BR />\n");
    print("<BR />\n");
  }
?>

To provide the same functionality of this script in LXP, the markup could look like this:

<lxp>
 <include src="host=localhost port=5432 dbname=somedb user=someuser" 
          sql="SELECT name, c_city FROM company_auth">
   Name: <field name="name" /><BR />
   City: <field name="c_city" /><BR />
   <BR />
 </include>
</lxp>

Now the power of LXP is starting to show through. Notice the simplicity of the LXP markup to generate the same output as the PHP code. Beyond limiting the amount of coding you have to do in LXP to achieve similar results as other languages, LXP also offers some unique features.

Unlike PHP, which will only provide persistent connections using pg_pConnect() (if there is an existing connection to reuse), LXP provides true persistent connectivity to PostgreSQL. For every instance of an Apache child, there is a connection to the PostgreSQL database. These persistent connections allow you to eliminate the startup penalties of starting a new PostgreSQL process to execute queries.

LXP also has a built-in XML parser for such tasks as parsing RSS and RDF content. RDF (Resource Description Format) is the standard XML format for delivering news (and other resources) to the Web. If you have ever visited TheLinuxReview (see resources below), Slashdot, or Meerkat, RDF is the format they use to import and/or export headlines.

If you wanted to use LXP to generate an HTML-based headline listing from an RDF file from Linux Weekly News, you could easily do so with the following snippet of code:

<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="2"><TR><TD bgcolor="#000000">
  <TABLE BORDER="0" CELLSPACING="1" CELLPADDING="0"><TR>
    <TD bgcolor="#8888a0" WIDTH="260">
      <A HREF="http://www.lwn.net/"><STRONG>Linux Weekly/Daily</STRONG></a>
    </TD>
  </TR>
  <TR>
    <TD bgcolor="#000000" WIDTH="260">
    
<lxp>
  <include src="/home/web/ports/headlines/lwn.rss" lastblock="3" method="rss">
          <TABLE BORDER="0" CELLSPACING="1" CELLPADDING="3" WIDTH="100%">
            <TR>
              <TD bgcolor="#ffffff" WIDTH="100%">
                - <field />
              </TD>
            </TR>
            <TR>
              <TD bgcolor="#e0e0e8" WIDTH="100%">
                <STRONG>
                  <field type="url" link="Read More..." target="_blank" />
                </STRONG>
                <BR />
              </TD>
            </TR>
          </TABLE>
  </include>
</lxp>
    
    </TD>
  </TR></TABLE>
</TD></TR></TABLE>

This would generate the following output:

Notice that the only actual LXP-specific portion of this markup is the following:

<lxp>
  <include src="/home/web/ports/headlines/lwn.rss" lastblock="3" method="rss">
    <field />
    <field type="url" link="Read More..." target="_blank" />
  </include>
</lxp>

Okay, LXP looks interesting, what can't it do?

LXP is designed to be a data/content markup language and not a "programming" language. In other words, in addition to its own capabilities, it is also designed to be a value-add or enabler for other languages. There are some tasks better left to PHP or another language. For example, LXP cannot send e-mail from an HTML-based form. PHP, on the other hand, can. This is where an example of LXP's sub-request functionality can be made. To send e-mail with PHP you could use code similar to the following:

<?
  mail("$email","$subject","Hello this is a test email from $email");
?>

You simply cannot do this in LXP natively. However, with sub-requests LXP can provide interaction with the PHP script and allow you to continue within your productive LXP environment. To include a PHP script within an LXP script you would use the following:

<lxp>
  <include src="mailto.php" method="uri" />
</lxp>

The URI (sub-request) method of LXP supports any known type to Apache, allowing for diverse brokering capabilities. Through the URI LXP method, LXP can pass variable values to PHP. This is powerful because it does not require the writing of obscure code or CGI wrappers to work within both environments.

If we take the above examples and expand on them, we can see how PHP and LXP can work together.

<lxp>
  <ifnot sendmail>
    <form method="post" action="http://stage.linuxports.com/test.lxp">
       Email Address: <input type="text" name="email" /> <BR />
       Subject:       <input type="text" name="subject" /> <BR />
       <input type="submit" name="sendmail" value="Email" />
    </form>
  </ifnot>
  <else>
    <include src="mailto.php" type="uri" />
  </else>
</lxp>

The source of the mailto.php file looks like this:

<title>My Email Form</title>
<?
  mail("$email","$subject", "Hello this is a test email from $email");
  print ("You just sent an email to $email");
?>

Over the next month, I will share more advanced articles on the use of LXP with PostgreSQL. Don't worry, though; these articles will not affect my ever-growing-in-popularity SysAdmin column, which millions of people joyously flock to every week to read.

More Stories By Joshua Drake

Joshua Drake is the co-founder of Command Prompt, Inc., a PostgreSQL and Linux custom development company. He is also the current author of the Linux Networking HOWTO, Linux PPP HOWTO, and Linux Consultants HOWTO. His most demanding project at this time is a new PostgreSQL book for O'Reilly, 'Practical PostgreSQL'

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.