Home Backend Development PHP Tutorial php生成WAP页面_php基础

php生成WAP页面_php基础

May 17, 2016 am 09:44 AM

WAP(无线通讯协议)是在数字移动电话、个人手持设备(PDA等)及计算机之间进行通讯的开放性全球标准。由于静态的WAP页面在很多方面不能满足用户个性化的服务请求,因此通过WAP服务器端语言产生动态的WML页面,具有很广泛的应用价值和很高的商业价值。

  WAP应用结构非常类似于Internet,一个典型的WAP应用请求是这样的:首先,具有WAP用户代理功能的移动终端(WAP手机等)通过内部运行的微浏览器(Micro Browser)对某一网站以无线方式发送WAP服务请求。该请求先由WAP网关截获,对信息内容进行编码压缩,以减少网络数据流量,同时根据需要将WAP协议转换成HTTP协议,然后将处理后的请求转送到相应WAP服务器。在WAP服务器端,根据页面扩展名等性质,被请求的页面直接或由服务器端脚本解释后输出,再经网关传回用户。

  从上述WAP应用流程可以看到,生成动态WAP页面与动态产生Web网页的过程非常类似。但是由于WAP应用使用的WML语言来源于语法严格的XML,因此要求输出的格式必须按WAP网页的规范输出。同时,由于WAP协议的应用范围、移动客户端的软硬件水平等特殊性,对每次输出的页面的大小、图像的格式及容量都有一定限制。下面我们以PHP脚本语言为例,看看如何动态输出WAP页面。

一、设置WEB服务器

  首先你的 Web服务器要安装好PHP,即能处理PHP脚本程序。其次,为使Web服务器能同时识别和处理PHP、WML、WBMP等文件,Web 服务器的MIME表需添加以下的几种文件类型。

  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

  二、用PHP输出简单动态WAP页面

  下面有一个最简单的PHP生成WAP页面的例子。注意由于需要PHP解释器来解释该程序,并输出WAP页面,因此所有类似程序应以.php为扩展名。

    header(″Content-type: text/vnd.wap.wml″);
  echo (″

″);
  echo date( ″l dS of F Y h:i:s A″ );
  echo (″

″);
  ?>

  该例子在WAP手机模拟器中可以浏览,输出当前日期时间,而在普通的浏览器中无法识别,甚至会被认为是错误下载。这是因为在程序开头就声明了该输出文档为WML类型,该类型只有WAP设备能够识别并解释。值得注意的是,我们常见的HTML语言对规范性要求不严,大多数浏览器能“容忍”其中相当多的编写错误,而WML规范相当严格,一点失误都可能导致无法输出所需页面。

  一旦我们知道了用PHP脚本输出WAP页面的标准过程,我们就能够使用PHP强大的功能配合以WML语言的交互处理以及WML Script的简单脚本,开发出适合我们需要的应用系统了。

三、用PHP动态生成图像

  WAP应用使用一种特殊黑白的图像格式WBMP。我们可以用一些工具来将已有图像转换成WBMP格式,然后在WML文档中使用。但是在WAP站点上如果能动态地生成所需图像如K线图等,将会有广阔的应用前景。幸运的是,PHP的GD库(版本1.8以上)已经提供了相应函数。

    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, 20, 20, Sblack);
  ImageWBMP(Sim);   ImageDestroy(Sim);
  ?>

  该文件将在WAP模拟器中显示一个黑色矩形框。注意要使用GD的图像函数库,必须在PHP配置中加载PHP_GD.DLL库文件。

  四、在PHP中处理汉字

  WAP作为一种全球应用,选择了UNICODE 2.0作为其标准字符集编码,以便能同时处理包括英文、中文、日文、法文等多种文字。而我们平常处理汉字使用的是GB2312编码,不同的内码标准势必不能通用,因此如果不在两种编码之间通过码表进行转换,就会出现汉字乱码现象。现在已经有较成熟的GB-2312与UNICODE编码转换的程序和函数,并在ASP、PHP、JSP等系统中使用,我们可以在一些技术站点上找到它们。

  目前的大多数WAP手机(Nokia7110、爱立信R320S等等)都是使用UTF-8编码的,也就是采用UNICODE来编码。这样,如果我们直接在WML使用中文字符(GB2312编码),将会产生乱码,手机用户无法识别,所以我们在输出中文之前,要使用程序或函数对中文进行UNICODE的编码。而在少数支持GB2312编码的手机或WAP终端设备中,我们可以在程序中定义好文档的内码类型后即可直接正确显示汉字,例如:

    header(″Content-type: text/vnd.wap.wml; charset=gb2312″);
  echo (″

″);
  echo (″中文测试″);
  echo (″

″);
  ?>  

原作者:不详  
来源:php自由联盟    

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 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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles