這個教程面向的是對于創(chuàng)建店面、自動連接 Amazon Web 服務(wù)并顯示結(jié)果有興趣的程序員。這個教程假設(shè)讀者熟悉基本的 PHP 概念,包括 for 和 while 循環(huán),以及表單處理。如果對自己的 PHP 能力沒把握,有許多優(yōu)秀的教程可以幫助您達(dá)到要求(請參閱 參考資料)。
您應(yīng)當(dāng)熟悉 Amazon Web 服務(wù),在 developerWorks 的 “Boost application development with Amazon Web Service” 系列文章中介紹了它(請參閱 參考資料)。
我們將構(gòu)建一個 Amazon 店面,包含分類鏈接和搜索框,允許購物者瀏覽商店中的商品目錄。與多數(shù) PHP 應(yīng)用程序不同,這個程序不需要本地數(shù)據(jù)庫,因?yàn)閿?shù)據(jù)庫保存在 Amazon 的服務(wù)器上。所以,這個教程嚴(yán)重地依賴簡單對象訪問協(xié)議(SOAP),這個 Web 服務(wù)協(xié)議用 XML 組織信息。Web 服務(wù)是應(yīng)用程序(例如在這個教程中要構(gòu)建的 PHP 應(yīng)用程序)與中央服務(wù)器通信以獲取信息的一種方式。
通過 Amazon 電子商務(wù)服務(wù)(ECS),可以從購物者選擇的分類中獲得和顯示內(nèi)容。當(dāng)選中一個分類時,將創(chuàng)建參數(shù),指明這個分類,收集相關(guān)信息,并啟動 SOAP 客戶。SOAP 客戶會接受參數(shù),形成 XML 文檔,并把 XML 文檔轉(zhuǎn)交給 SOAP 服務(wù)器。Amazon 服務(wù)器訪問自己的數(shù)據(jù)庫,生成 XML 文檔,里面包含的商品與參數(shù)匹配。最后,服務(wù)器把 XML 文檔返回給 SOAP 客戶,SOAP 客戶再把文檔解析成數(shù)據(jù)結(jié)構(gòu),從數(shù)據(jù)結(jié)構(gòu)中可以提取出請求的數(shù)據(jù)。
服務(wù)器為每個請求類型返回的數(shù)據(jù)都是相同的。但是,請求的設(shè)置和使用都有不同。REST 請求最簡單,因?yàn)樗鼈冎皇情L長的 URL,URL 中包含每個商品的變量和它們的值 —— 非常像 PHP 中的 GET 請求。
另一方面,SOAP 請求更復(fù)雜,因此也就更強(qiáng)大。它們是通過 HTTP 傳遞給 SOAP 服務(wù)器的 XML 文檔。SOAP 請求比 GET 請求更強(qiáng)大,因?yàn)樗鼈兡芙邮芨鄥?shù),而且多個請求也可以綁定在一個 SOAP XML 文檔中,這樣就支持同時請求。而且,SOAP 請求擁有優(yōu)先級,這就增強(qiáng)了商店整體的響應(yīng)性。
<?php
...
$title="Welcome to TylerCo.";
require(‘header.php‘);
...
print("
<p>Welcome to TylerCo., where you
will find great value and great service!
Guaranteed!<p> Please browse the
products on our Web site by clicking
on one of the categories to the left");
...
require(‘footer.php‘);
?>
在這里輸出了標(biāo)題,而且如果需要的話,還會輸出主鏈接。如果 Web 瀏覽器被指向 storeFront.php,而不是某個關(guān)鍵字或分類,那么只會顯示 Home 這個文本。但是,如果正在瀏覽分類,那么會顯示這個鏈接,在鏈接上點(diǎn)擊,會清空 GET 字符串中的變量并回到主頁。
...
require(‘header.php‘);
if($_GET[‘category‘] != ‘‘){
printCategoryItems();
}
else{
print("
<p>Welcome to TylerCo., where you will find
great value and great service!
Guaranteed!<p>
Please browse the products on our Web site by clicking
on one of the categories to the left");
}
function printPrevNextPage($totalPages, $pagenum){
$previousPage = processPrevPage();
$nextPage = processNextPage($totalPages);
print("<table width=‘100%‘><tr><td width=‘33%‘
align=‘left‘>");
print("$previousPage</td>");
print("<td width=‘33%‘ align=‘center‘>Page ".$pagenum);
print(" of $totalPages</td>");
print("<td width=‘33%‘ align=‘right‘>$nextPage
</td></tr></table>");
print("<font size=‘0‘><br><center>The
above information came from ");
print("Amazon.com. We make no guarantees to the accuracy of the above ");
print("information.</center></font>");
}
清單 26 中的函數(shù)調(diào)用兩個子函數(shù):processPrevPage() 和 processNextPage()。這個函數(shù)接受兩個返回的鏈接,并顯示它們,這樣就允許客戶在頁面間游歷。它還顯示了一個免責(zé)聲明,表明 Web 站點(diǎn)的作者對于 Amazon 提供的信息不承擔(dān)責(zé)任。
...
$result = callSOAPFunction("ItemSearch", $array);
if($result->faultstring || $result->Items->Request->Errors){
error("Your search return 0 results, or perhaps there was an
error processing your request. The returned XML file with
additional error information is as follows:<br><br>",
$result->Items->Request->Errors->Error, $result);
}
else{
$items = $result->Items->Item;
if($items){
printItemsSection($innerTitle, $items);
printPrevNextPage($result->Items->TotalPages, $pagenum);
}
}
...