search
HomeBackend DevelopmentPHP Tutorial用PHP编程语言开发动态WAP页面_PHP

文/成都 yell

  责任编辑:大曾

  WAP(无线通讯协议)是在数字移动电话、个人手持设备(PDA等)及计算机之间进行通讯的开放性全球标准协议。随着无线通讯的不断发展,静态的WAP页面在很多方面已经不能满足用户个性化的要求,因此开发者可以在WAP服务器端使用诸如PHP等语言产生动态的WML页面,来满足用户的需要。


   WAP的应用结构非常类似于Internet,一个典型的WAP应用请求步骤描述如下:

   1. 具有WAP用户代理功能的移动终端(如WAP手机),通过内部运行的微浏览器向某一网站发送WAP服务请求。该请求先由WAP网关截获,对信息内容进行编码压缩,以减少网络数据流量,同时根据需要将WAP协议转换成HTTP协议。

   2. 协议将处理后的请求转送到相应WAP服务器。在WAP服务器端,根据页面扩展名等属性,被请求的页面直接或由服务器端脚本解释后输出,再经过网关传回给用户。

  从上述的WAP应用流程可以发现,生成动态WAP页面与动态产生Web网页的过程非常相似。但是由于WAP应用使用的WML语言来源于语法严格的XML,因此要求输出的格式必须按WAP网页的规范输出。同时,由于WAP协议的应用范围及移动客户端的软、硬件配置等局限性,对每次输出的页面的大小、图像的格式及容量都有一定限制。本文笔者将以PHP语言为例,和广大网络程序开发爱好者共同探讨动态输出WAP页面的方法和应用。

  输出简单的动态WAP页面

  由于生成WAP页面的过程和生成一般的Web页面非常类似,笔者通过一个最简单的WAP页面的例子来介绍。不过提醒一句:由于需要PHP解释器来解释该程序并输出WAP页面,因此所有类似的程序应以“php”为扩展名哦。

  
  header("Content-type: text/vnd.wap.wml"); //定义输出文档为WML类型

  echo (" ");

  echo ( "Hello WAP" );

  echo ("");

  ?>

  该实例可以在WAP手机模拟器中浏览,输出一句经典的“Hello WAP”语句,但是在普通的网络浏览器中是无法识别的,原因很简单,在程序开头声明了该输出文档为WML类型,只有WAP设备能够识别并解释。不过又要提醒一句:常见的HTML语言对规范性要求不严,大多数浏览器能“宽容”地接受其中的编写错误,但是WML的规范相当严格,任何的错误都可能导致无法输出所需的页面。

   实例1 动态生成图像

  WAP使用的图像是一种特殊的黑白图像格式:WBMP。开发者可以使用一些现有的工具将一般图像转换成WBMP格式,然后在WML文档中使用。但是如果在WAP程序中能动态地生成所需图像(如股市的K线图),将会使程序有极其广阔的应用前景。PHP提供了强大的图形绘制功能,下面的实例将将在WAP模拟器中显示一个黑色的矩形框。

   (注意:要使用GD的图像函数库,必须在PHP配置中加载“PHP_GD.DLL”库文件。)

  
  Header("Content-type: image/vnd.wap.wbmp"); //定义输出的图像格式为WBMP

  Sim = ImageCreate(50, 50);

  Swhite = ImageColorAllocate(Sim,255,255,255);

  Sblack = ImageColorAllocate(Sim,0,0,0);

  ImageRectangle(Sim, 5, 5, 20, 20, Sblack);

  ImageWBMP(Sim);

  ImageDestroy(Sim);

  ?>

  实例2 处理汉字

  作为一种全球应用的协议,WAP选择了UNICODE 2.0作为其标准字符集编码,能够同时处理英、中、日、法等多种文字。但是开发者日常处理的汉字是GB2312编码,不同的内码标准必定不能通用,因此,如果不在两种编码之间通过码制的转换,就会出现汉字乱码的现象。目前的大多数WAP手机(Nokia7110、爱立信R320S等等)都使用UTF-8(即UNICODE)来编码的。如果直接在WML使用中文字符(GB2312编码),将会产生乱码,导致手机用户无法识别,因此在输出中文之前,必须使用程序或函数(关于此类的PHP函数库,网络上已有非常多技术上成熟的产品可以下载)对中文进行UNICODE的编码。而在少数支持GB2312编码的手机或WAP终端设备中,开发者只需在程序中定义文档的内码类型后,即可直接正确显示汉字,看一个实例:

  
  header("Content-type: text/vnd.wap.wml; charset=gb2312"); //定义字符的编码为GB2312

  echo ("");

  echo ("你好");

  echo ("");

  ?>

   在程序的“header”语句中,定义了文字的编码为GB2312,如果用户的手机支持GB2312编码,将会显示“你好”的字样。

  作为未来网络通讯的主导,WAP程序的开发已日趋热门。相信通过本文的阅读,能使开发者对使用PHP进行WAP开发有一个初步的印象,希望广大读者能在本文的基础上,参考WML语言,开发出更加强大的WAP应用程序。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Beyond the Hype: Assessing PHP's Role TodayBeyond the Hype: Assessing PHP's Role TodayApr 12, 2025 am 12:17 AM

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

What are Weak References in PHP and when are they useful?What are Weak References in PHP and when are they useful?Apr 12, 2025 am 12:13 AM

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

Explain the __invoke magic method in PHP.Explain the __invoke magic method in PHP.Apr 12, 2025 am 12:07 AM

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

Explain Fibers in PHP 8.1 for concurrency.Explain Fibers in PHP 8.1 for concurrency.Apr 12, 2025 am 12:05 AM

Fibers was introduced in PHP8.1, improving concurrent processing capabilities. 1) Fibers is a lightweight concurrency model similar to coroutines. 2) They allow developers to manually control the execution flow of tasks and are suitable for handling I/O-intensive tasks. 3) Using Fibers can write more efficient and responsive code.

The PHP Community: Resources, Support, and DevelopmentThe PHP Community: Resources, Support, and DevelopmentApr 12, 2025 am 12:04 AM

The PHP community provides rich resources and support to help developers grow. 1) Resources include official documentation, tutorials, blogs and open source projects such as Laravel and Symfony. 2) Support can be obtained through StackOverflow, Reddit and Slack channels. 3) Development trends can be learned by following RFC. 4) Integration into the community can be achieved through active participation, contribution to code and learning sharing.

PHP vs. Python: Understanding the DifferencesPHP vs. Python: Understanding the DifferencesApr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: Is It Dying or Simply Adapting?PHP: Is It Dying or Simply Adapting?Apr 11, 2025 am 12:13 AM

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The Future of PHP: Adaptations and InnovationsThe Future of PHP: Adaptations and InnovationsApr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.