March 2, 2021
Hot Topics:

WML Scripting Tips and Integration with PHP

  • By Steve Schafer
  • Send Email »
  • More Articles »

Integrating PHP into WML

Although WML is well suited to most mundane content delivery tasks, it falls short of being useful for database integration or extremely dynamic content.

Another Web technology, PHP, fills this gap quite nicely-integrating into most databases and other Web structures and languages. It's possible to "cross-breed" mime types in Apache and IIS to enable PHP to deliver WML content. The following sections show you how to configure your server and how to integrate the two technologies.

Note: This section assumes that you have a basic understanding of PHP. To learn more about PHP, visit the PHP Web site (www.php.net) and browse through the language documentation. You can also find a series of articles here on Developer.com for learning PHP. The first is Learning PHP: The What's and the Why's

What Is PHP?

According to the PHP documentation, "PHP (recursive acronym for 'PHP: Hypertext Preprocessor') is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML."

Essentially, this scripting language is a server-side language that's processed before being sent to the requesting client. This is in contrast to scripting languages like JavaScript that are client-side and processed by the client's browser after being sent to the client.

The benefit of being a preprocessed language is that PHP pages can be highly dynamic. Decisions can be made from user input, databases, server conditions, and so on about how the requested content is delivered to the client. It's exactly what we need to increase the power of our WML.

Installing PHP

PHP is available for most major Web server platforms, including Apache and Microsoft's Internet Information Server (IIS). Visit www.php.net, download the appropriate version for your server, and install it by following the included instructions.

Note: If you're running Debian, Red Hat, or another version of Linux with a robust packaging system, PHP is probably available in a preconfigured package. Check your distribution's Web site and documentation for more info on PHP package(s).

Cross-breeding with WML: Apache

To enable WML pages to be parsed by PHP, you simply have to add the WML suffix to the PHP application definition in your Apache configuration file:

    AddType application/x-httpd-Php .php .wml

Note the addition of ".wml" to the definition. This will cause the server to parse all .wml files with PHP. Don't forget to restart Apache so it will read the new configuration. Make sure that your WML definitions appear before the PHP definition to ensure that Apache recognizes ".wml" as a valid type.

Tip: As mentioned above, this causes all .wml files to be parsed by PHP. Although this is usually desirable, you could create another mime type ("pwml") and include it in both the WML and PHP definitions. Then you could continue to use .wml for pure WML files and the new type for PHP-parsed WML.

Cross-breeding with WML: IIS

To enable WML pages to be parsed by PHP, you need to add PHP as an application to handle your .wml files.

To do this, follow the steps below:

  1. Open the Internet Information Services Management Console.

  2. Right-click on the site where you want to add this functionality (or use Default Web Site) and choose Properties.

  3. Access the Home Directory tab and click the Configuration button near the bottom of the dialog.

  4. Click the Add button to add an application.

  5. Add the path and filename of the PHP executable (usually "C:\PHP\php.exe") in the Executable field and ".wml" in the Extension field.

  6. Ensure that "Script Engine" and "Check that file exists" are both checked.

  7. Select OK back to the IIS MC.

  8. Although not usually required, you may need to restart your Web server.

In step 6, above, setting the "Check that file exists" option is not entirely necessary, but can be helpful in debugging server issues. Without that option being set, IIS will pass all requests for .wml pages to PHP, even if the requested page doesn't exist. Instead of generating a "404 - Not Found" error, the Web server will report a problem with PHP (which failed because there was no file to parse).

These steps will result in all .wml files being parsed by PHP. Although this is usually desirable, you could create another mime type ("pwml") and include it in the WML definition and define PHP as an application for the new type. Then you could continue to use .wml for pure WML files and the new type for PHP-parsed WML.

Getting the Client to Recognize PHP-parsed WML

Now that the server has been configured to deliver PHP-parsed WML, you need to ensure that the client will receive it as WML. This is accomplished by having each PHP-parsed page send the appropriate header to the client.

You should begin each PHP file with the following line:

    header("Content-type: text/vnd.wap.wml");

This line needs to come before anything else is sent to the client-it pays to make a habit of including it right after the "<?php" start tag.

The following example shows how to display a one-card deck with the text "Hello world" using PHP:


header("Content-type: text/vnd.wap.wml");  

print "<?xml version=\"1.0\"?>\n";  
print "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\""  
   . " \"http://www.wapforum.org/DTD/wml_1.1.xml\">\n"; 
print "<wml>\n";
print "<card>\n";
print "<p>Hello world</p>\n";
print "</card>\n";
print "</wml>\n";


Note that there are several ways to accomplish sending the WML to the client. I've chosen to use individual print statements, each ending with a newline code. This keeps the resulting WML fairly readable and PHP mimics the line breaks. In some cases, namely long stretches of code, a print "here document" structure would work better. The above example would resemble the following in "here document" structure:

print <<<END
<?xml version=\"1.0\"?>
<p>Hello world</p>

In any case, remember that your target platform is WML-stick to WML-compatible tags and structure and avoid using reserved characters (such as "$") in places where they might be misinterpreted. Also, debugging the code is twice as hard as straight WML because you have to debug the PHP code as well as the resulting WML. The next article in this series will provide more debugging tips.

Page 2 of 3

This article was originally published on August 19, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date