Using PHP & WAP part I
By Andrew Spiers
Get a text version of this document
PHP provides a powerful language which can be used to create dynamic WML page. Here we will look at a few simple ideas that may help you when you are creating PHP scripts.
Lets have a look at the following script :
<?
header("Content-type: text/vnd.wap.wml");
echo("<?xml version=\"1.0\"?>\n");
echo("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum/DTD/wml_1.1.xml\">\n\n");
echo "<wml>";
echo "<card id=\"card1\">";
echo "<p>";
echo "Hello World";
echo "</p>";
echo "</card>";
echo "</wml>";
?>
A simple PHP script that displays "Hello World". This script is a bit over the top as PHP enables you to 'switch' from PHP to WML/HTML within the same script so the above could be simplified to :
<?
header("Content-type: text/vnd.wap.wml");
echo("<?xml version=\"1.0\"?>\n");
echo("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum/DTD/wml_1.1.xml\">\n\n");
?>
<wml>
<card id="card1">
<p>
Hello World
</p>
</card>
</wml>
As you can see, slightly easier to write as you don't have to worry about 'escaping' special characters like ".
Looking at the following example (which displays the time in 24hr format), it goes 'in and out' of PHP twice :
<?
header("Content-type: text/vnd.wap.wml");
echo("<?xml version=\"1.0\"?>\n");
echo("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum/DTD/wml_1.1.xml\">\n\n");
?>
<wml>
<card id="card1">
<p>
Time is <?echo date("G:i");?>
</p>
</card>
</wml>
As you can see from the above examples, all PHP/WML scripts start with the same three lines :
header("Content-type: text/vnd.wap.wml");
echo("<?xml version=\"1.0\"?>\n");
echo("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum/DTD/wml_1.1.xml\">\n\n");
But what happens if you want to change it to WML 1.2 ? You would have to go and change all of your PHP files. A way round this is to put those three lines into a seperate file, for example wapheader.php, which would look like this :
<?
header("Content-type: text/vnd.wap.wml");
echo("<?xml version=\"1.0\"?>\n");
echo("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum/DTD/wml_1.1.xml\">\n\n");
?>
This file could then be included into your script, so our time script would now look like this :
<? include("wapheader.php");?>
<wml>
<card id="card1">
<p>
Time is <?echo date("G:i");?>
</p>
</card>
</wml>
This technique of using templates could be used at any level, so if you had a help card which was used across several decks then you could seperate this out into a template file and then include it in your deck as required.
It is possible to pass variables into included files so if your include file was this :
<?
header("Content-type: text/vnd.wap.wml");
echo("<?xml version=\"1.0\"?>\n");
echo("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum/DTD/wml_1.1.xml\">\n\n");
?>
<wml>
<card id="<?echo $cardid;?>">
Then you could have the following script :
<?
$cardid = "card1";
include("wapheader.php");
?>
<p>
Time is <?echo date("G:i");?>
</p>
</card>
</wml>
Which would output a deck of :
<?xml version=\"1.0\"?>
<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum/DTD/wml_1.1.xml\">
<wml>
<card id="card1">
<p>
Time is 09:00
</p>
</card>
</wml>
Hope these ideas help and you can use them in your PHP scripts.
|