search

PHP与WAP_PHP

Jun 01, 2016 pm 12:34 PM
wapuseprogramcodingoutputpage

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
PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 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.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor