能上網的手機越來越多,若隨時能從手機看到自己的網站資訊,那自是好事一樁。動手做其實不難。
打開 httpd.conf,在檔案最後加上 WAP 相關的 MIME 類型對應:
AddType text/vnd.wap.wml .wml
AddType application/vnd.wap.wmlc .wmlc
AddType image/vnd.wap.wbmp .wbmp
AddType application/vnd.wap.wmlscriptc .wmlsc
AddType text/vnd.wap.wmlscript .wmls
AddType application/vnd.wap.wmlscriptc .wsc
存檔後讓設定生效(重啟或 reload httpd)。就這樣,Apache 已經具備提供 WAP 內容的能力。
拿手機開 https://我的網站/index.php,你會發現根本讀不出來。原因很簡單:WAP 瀏覽器不是網頁瀏覽器,整個手機的容量可能還不到一個網頁瀏覽器軟體。要走 WAP,就得依 WAP 的規則,用 WML 寫頁面。
在站點根目錄建立 wap 目錄,其中放一個 test.wml,內容大致如下:
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "https://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<head>
<meta forua="true" http-equiv="Cache-Control" content="max-age=0"/>
<meta forua="true" http-equiv="Cache-Control" content="must-revalidate"/>
</head>
<template>
<do type="prev"><prev/></do>
</template>
<card id="card1" title="wap" newcontext="true">
<p>輸入:<br/>
<input name="SID" size="10" maxlength="50" type="text" format="*N" emptyok="true"/><br/><br/>
<anchor title="login">
<go href="request.asp" method="post">
<postfield name="code" value="$SID"/>
</go>送出<br/>
</anchor>
</p>
</card>
</wml>
幾個關鍵點:XML 宣告中的編碼要與實際檔案一致(這裡用 GB2312);head 中的 Cache-Control 設定用來避免手機快取舊頁面,除錯時特別有用;template 中的 do type="prev" 提供返回上一張卡的功能;card 是 WML 的基本畫面單位;$SID 這種寫法會在送出時被替換為輸入值。
用手機打開 https://我的網站/wap/test.wml,這次就能正常顯示了。
關於 WML 語法這裡不多贅述,網路上資料很多。掌握規則之後,就可以用 PHP 或 CGI 產生動態的 WML 頁面,把伺服器端的資訊處理成 WML 輸出——例如系統負載、日誌摘要,都能做成手機上可讀的小頁面。