Programming | dynamic | page
WAP (Wireless Communication Protocol) is an open global standard protocol for communication between digital mobile phones, personal handheld devices (PDAs, etc.) and computers. With the continuous development of wireless communications, static WAP pages in many ways can not meet the user's personalized requirements, so developers can use in the WAP server, such as PHP and other languages to generate dynamic WML page, to meet the needs of users.
The application structure of WAP is very similar to the Internet, a typical WAP application request step is described as follows:
1. A mobile terminal (such as a WAP phone) with a WAP user agent function that sends a WAP service request to a Web site through an in-house micro-browser. This request is intercepted by WAP gateway, encoding the content of the information to reduce the traffic of network data, and convert the WAP protocol to HTTP protocol as needed.
2. The protocol transmits the processed request to the corresponding WAP server. On the WAP server side, according to the attributes such as the page extension, the requested page is exported directly or by the server-side script, and then passed back to the user through the gateway.
From the above WAP application process, we can find that the process of generating dynamic WAP pages is very similar to that of dynamically generating web pages. However, since the WML language used in WAP applications originates from the strict syntax of XML, the required output format must be exported according to the specification of WAP Web pages. At the same time, because of the application scope of WAP protocol and the limitation of software and hardware configuration of mobile client, there are some restrictions on the size of each output page, the format and the capacity of the image. In this paper, the author will take the PHP language as an example, and the vast number of network program development enthusiasts to explore the dynamic output WAP page method and application.
Output a simple dynamic WAP page
Because the process of generating a WAP page is very similar to generating a typical Web page, the author introduces it through one of the simplest examples of WAP pages. But the caveat: because you need a PHP interpreter to interpret the program and output a WAP page, all similar programs should be "PHP" extensions.
<?php
Header ("CONTENT-TYPE:TEXT/VND.WAP.WML"); //define output document as WML type
Echo ("");
Echo ("Hello WAP" );
Echo ("");
?>
This example can be browsed in WAP phone simulator, output a classic "Hello WAP" statement, but in the ordinary Web browser is not recognized, the reason is very simple, at the beginning of the program declared that the output document is WML type, only WAP devices can recognize and explain. But another caveat: the common HTML language is not strict with the normative requirements, most browsers can "tolerate" the written error, but the WML specification is very strict, any error may result in the inability to output the required page.
Instance 1 dynamically generating images
The image used by WAP is a special black-and-white image format: WBMP. Developers can use some of the existing tools to convert generic images into WBMP formats and then use them in WML documents. However, if the required images can be generated dynamically in the WAP program (such as the K-line chart of the stock market), the program will have an extremely broad application prospect. PHP provides a powerful graphical rendering function, and the following example displays a black rectangular box in the WAP emulator.
(Note: To use the image Library of GD, you must load the PHP_GD in your PHP configuration.) DLL "library file. )
<?php
Header ("Content-type:image/vnd.wap.wbmp"); //define output image format as Wbmp
$im = imagecreate (M);
$white = Imagecolorallocate ($im, 255,255,255);
$black = Imagecolorallocate ($im, 0,0,0);
Imagerectangle ($im, 5, 5, $black);
Imagewbmp ($im);
Imagedestroy ($im);
?>
Example 2 processing Chinese characters
As a global application protocol, WAP chooses UNICODE 2.0 as its standard character set code, which can simultaneously deal with many languages, such as English, Chinese, Japanese and French. But developers daily processing of Chinese characters is GB2312 code, different internal code standards must not be universal, therefore, if not between the two codes through the conversion of code system, there will be the phenomenon of Chinese characters garbled. Most of the current WAP phones (Nokia7110, Ericsson R320s, and so on) are encoded using UTF-8 (UNICODE). If you are using the Chinese character (GB2312 encoding) directly in WML, will be garbled, resulting in the cell phone users can not recognize, so before the output of Chinese, must use the program or function (about this class of PHP library, the network has a very many technologically mature products can be downloaded) to the Chinese UNICODE The encoding. In a few mobile phones or WAP terminals that support GB2312 encoding, the developer can display Chinese characters directly after defining the code type of the document in the program, and look at an example:
<?php
Header ("CONTENT-TYPE:TEXT/VND.WAP.WML; charset=gb2312 "); ///define character encoding for GB2312
Echo ("");
Echo ("Hello");
Echo ("");
?>
In the "header" Statement of the program, the encoding of the text is defined as GB2312, and if the user's phone supports GB2312 encoding, the words "Hello" will be displayed.
As the leading of network communication in the future, the development of WAP program is becoming more and more popular. I believe that through this reading, developers can use PHP for WAP development has a preliminary impression, I hope that the majority of readers in this article based on the WML language, the development of a more powerful WAP applications.