Home php教程 php手册 用 PHP 编程语言开发动态 WAP 页面

用 PHP 编程语言开发动态 WAP 页面

Jun 21, 2016 am 08:58 AM
echo php wap

编程|动态|页面

  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
$im = ImageCreate(50, 50
);
$white = ImageColorAllocate($im,255,255,255
);
$black = ImageColorAllocate($im,0,0,0
);
ImageRectangle($im, 5, 5, 20, 20, $black
);
ImageWBMP($im
);
ImageDeStroy($im
);
?>

  实例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 of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 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.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles