WAP(Wireless Application Protocol)早期為功能機設計,標記語言為 WML(基於 XML,語法嚴格)。雖智能機普及已轉向 HTML5,但物聯網、工控終端、舊系統維護仍需 WML 知識。
.php 解析腳本,輸出 WML 內容。<?php
header('Content-Type: text/vnd.wap.wml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "https://www.wapforum.org/DTD/wml_1.1.xml">';
?>
<wml>
<card id="main" title="WAP Demo">
<p>Hello WAP! 當前時間:<?php echo date('H:i:s'); ?></p>
<p><a href="menu.wml">進入菜單</a></p>
</card>
</wml>
關鍵點:必須輸出 XML 聲明與 DOCTYPE,任何語法錯誤導致終端顯示「文檔格式錯誤」。
<?php
header('Content-Type: image/vnd.wap.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);
需啟用 GD 擴展(extension=gd)。WBMP 僅支援 1bit 黑白,複雜圖表建議服務端預渲染為 PNG 再轉換。
header('Content-Type: text/vnd.wap.wml; charset=utf-8');
$text = '你好 WAP';
echo mb_convert_encoding($text, 'UTF-8', 'GB2312');
header('Content-Type: text/vnd.wap.wml; charset=gb2312');
echo '你好 WAP'; // 來源文件需為 GB2312 編碼
建議統一內部使用 UTF-8,輸出時根據 $_SERVER['HTTP_ACCEPT_CHARSET'] 動態轉碼。
<wml>
<card id="login" title="登錄">
<p>用戶名:<input name="user" type="text"/></p>
<p>密碼:<input name="pwd" type="password"/></p>
<p><anchor>提交<go href="login.php" method="post">
<postfield name="user" value="$(user)"/>
<postfield name="pwd" value="$(pwd)"/>
</go></anchor></p>
</card>
</wml>
PHP 端用 $_POST['user'] 接收。會話靠 PHPSESSID 透過 URL 參數或 Cookie 傳遞(視閘道器支援)。