amfphp下載:
http://sourceforge.net/projects/amfphp/files/amfphp/
這里我們下載
amfphp 1.9.zip
下載后解壓到web服務(wù)器的工作目錄下,前提是您已配置好php的工作環(huán)境。這里為了簡(jiǎn)單起見(jiàn),使用IIS7.0+php5.2
即:將amfphp1.9解壓到C:\inetpub\wwwroot

browser:目錄為amfphp可供我們直接在瀏覽器瀏覽的目錄
services:目錄是我們自己開(kāi)發(fā)的php類(lèi)文件存放目錄
gateway.php是一個(gè)比較重要的文件。
打開(kāi)gateway.php,定位到127行
//$gateway->setCharsetHandler("utf8_decode", "ISO-8859-1", "ISO-8859-1");
$gateway->setCharsetHandler("utf8_decode", "utf-8", "utf-8");
設(shè)置中文字符支持
如果服務(wù)器和php環(huán)境正常的話,在地址欄輸入
http://localhost/amfphp/browser/將會(huì)看到如下圖所示

配置參數(shù)如上圖所示,點(diǎn)save保存設(shè)置。
編寫(xiě)一個(gè)php與mysql交互的類(lèi)。
product.php
<?php
class product{
function print_xml(){
//獲取數(shù)據(jù)庫(kù)連接
$link=@mysql_connect("localhost","root","") or die('數(shù)據(jù)庫(kù)連接錯(cuò)誤');
//選擇數(shù)據(jù)庫(kù)
mysql_select_db("compass",$link);
//設(shè)置數(shù)據(jù)庫(kù)編碼
mysql_query("set names utf8",$link);
//查詢數(shù)據(jù)庫(kù)
$result=mysql_query("select * from product");
//創(chuàng)建DOMDocument對(duì)象
$doc = new DOMDocument('1.0','utf-8');
//格式化輸出
$doc->formatOutput = true;
//創(chuàng)建根元素
$root = $doc->createElement('root');
//添加根元素
$root = $doc->appendChild($root);
//從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)每一條是一個(gè)product
while($data=mysql_fetch_assoc($result)){
//創(chuàng)建product標(biāo)簽
$product=$doc->createElement('product');
//添加product標(biāo)簽
$product = $root->appendChild($product);
//創(chuàng)建Id元素
$id = $doc->createElement('id');
//添加Id
$id = $product->appendChild($id);
//創(chuàng)建文本內(nèi)容
$idtext = $doc->createTextNode($data['id'].'');
//將文本添加到id標(biāo)簽內(nèi)
$idtext = $id->appendChild($idtext);
//創(chuàng)建name標(biāo)簽
$name = $doc->createElement('name');
//添加name
$name = $product->appendChild($name);
//創(chuàng)建name標(biāo)簽的文本
$nametext = $doc->createTextNode($data['name'].'');
//設(shè)置name標(biāo)簽的文本
$nametext = $name->appendChild($nametext);
//創(chuàng)建price標(biāo)簽
$price = $doc->createElement('price');
//添加price
$price = $product->appendChild($price);
//創(chuàng)建price標(biāo)簽的文本
$pricetext = $doc->createTextNode($data['price'].'');
//設(shè)置price標(biāo)簽的文本
$pricetext = $price->appendChild($pricetext);
}
//關(guān)閉數(shù)據(jù)庫(kù)連接
mysql_close($link);
//保存xml
return $doc->saveXML();
}
}
?>
注意該文件的編寫(xiě)規(guī)則及存放路徑
php中類(lèi)文件的編寫(xiě)符合java中類(lèi)的編寫(xiě),即文件名與類(lèi)名大小寫(xiě)一致
該文件必須存放于C:\inetpub\wwwroot\amfphp\services\目錄下
方法最后使用return 返回而不是輸出
在
http://localhost/amfphp/browser/中的瀏覽情況