PHP Tutorials. Application Example 5

Source: Internet
Author: User
Tags date format character set echo date header install php php script
Tutorials | Application instance PHP generate WAP page
WAP (Wireless Communication Protocol) is an open global standard for communication between digital mobile phones, personal handheld devices (PDAs, etc.) and computers. Because the static WAP page can not satisfy the user's personalized service request in many aspects, it has a wide application value and high commercial value to produce dynamic WML pages through the WAP server-side language.

WAP application architecture is very similar to the Internet, a typical WAP application request is as follows: first, mobile terminals with WAP user agents (WAP phones, etc.) wirelessly send WAP service requests to a Web site via an Micro Browser. This request is intercepted by WAP gateway, encode the content of the information to reduce the traffic of network data, Convert WAP protocol to HTTP protocol as needed, and then transfer the processing request to the corresponding WAP server. On the WAP server side, according to the nature of the page extension, the requested page is exported directly or by the server-side script, and then the user is passed back through the gateway.

From the above WAP application process, we can see 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, the level of software and hardware of mobile client, the size of each output page, image format and capacity are limited. Let's take the PHP scripting language for example to see how to dynamically output a WAP page.

First, set up a Web server

First of all, your Web server to install PHP, that can handle PHP script program. Second, to enable the Web server to recognize and process PHP, WML, and wbmp files at the same time, the MIME table for the Web server needs to add the following file types.

TEXT/VND.WAP.WML. WML
Image/vnd.wap.wbmp. wbmp
APPLICATION/VND.WAP.WMLC. WMLC
Text/vnd.wap.wmls.wmls
APPLICATION/VND.WAP.WMLSC. WMLSC

Second, with PHP output simple dynamic WAP page

Here is an example of the simplest PHP-generated WAP page. Note that because you need a PHP interpreter to interpret the program and output a WAP page, all similar programs should have the. php extension.

<?php
Header (″content-type:text/vnd.wap.wml″);
Echo (″<wml> <card> <p>″);
echo Date (″l DS of F Y h:i:s A″);
Echo (″</p></card></wml>″);
?>

This example can be browsed in the WAP phone simulator, output the current date time, and in the ordinary browser is not recognized, and may even be considered a wrong download. This is because the output document is declared as a WML type at the beginning of the program, and only the WAP device can recognize and interpret it. It is worth noting that our common HTML language is not stringent requirements, most browsers can "tolerate" a considerable number of writing errors, and the WML specification is very strict, a bit of error can lead to the inability to output the required pages.

Once we know the standard process of exporting WAP pages with PHP scripts, we can use PHP's powerful capabilities to work with the WML language interaction and simple WML scripts to develop the application system that suits our needs.

Iii. dynamically generating images with PHP

WAP applications use a special Black-and-white image format wbmp. We can use some tools to convert existing images into wbmp format and then work with them in WML documents. However, if we can dynamically generate the required images such as chart on the WAP site, there will be a wide application prospect. Fortunately, PHP's GD library (version 1.8 or more) already provides the appropriate function.

? Php
Header (″content-type:image/vnd.wap.wbmp″);
Sim = Imagecreate (50, 50);
Swhite = Imagecolorallocate (sim,255,255,255);
Sblack = Imagecolorallocate (sim,0,0,0);
Imagerectangle (Sim, 5, 5, Sblack,);
Imagewbmp (Sim); Imagedestroy (Sim);
?>

The file will display 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.

Iv. handling Chinese characters in PHP

As a global application, WAP has chosen Unicode 2.0 as its standard character set encoding so that it can simultaneously handle multiple languages, including English, Chinese, Japanese, and French. And we usually deal with the use of Chinese characters is GB2312 encoding, the different internal code standards will not be universal, so if not between the two codes through the Code table to convert, the phenomenon of Chinese characters garbled. Now there are more mature GB-2312 and Unicode code conversion programs and functions, and in ASP, PHP, JSP and other systems used, we can find them on some technical sites.

Most of the current WAP phones (Nokia7110, Ericsson r320s, etc.) are encoded using the UTF-8 code, which is encoded using Unicode. In this way, if we use Chinese characters (GB2312 encoding) directly in WML, the code will be garbled and the cell phone user cannot recognize it, so we will use the program or function to encode the Chinese in Unicode before we export the language. In a few mobile phones or WAP terminals that support GB2312 encoding, we can directly display Chinese characters correctly after we have defined the code type of the document in the program, for example:

<?php
Header (″CONTENT-TYPE:TEXT/VND.WAP.WML; charset=gb2312″);
Echo (″<wml><card><p>″);
Echo (″ Chinese test ″);
Echo (″</p></card></wml>″);
?>





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.