Những trang web bán hàng trên mạng thường có một web service nó sẻ là một máy chủ phân tích XML xquery. Khi bạn gởi yêu cần đến webservice của nó thì nó sẻ lọc và trả kết quả ra dạng XML bạn chỉ cần biết cú pháp của nó thì sẻ lấy được tất cả thông tin thôi.
Điều đầu tiên là phải vào http://www.amazon.com/gp/browse.html?node=3435361 để đăng ký một tài khoảng. Nó sẻ cung cấp cho bạn số ID và bạn chỉ việc thế vào bên dưới là bạn đã có những gì mình cần rồi.
<?php
define('KEYID','18Q1XW9SAD57XJ06A982');// key nay ban co the len web service cua amazone dang ky mot cai th la no se cho minh ban the cai key nay cua minh bang key cua ban la xong.
define('AssocTag','YourAssociateTagHere');//cai nay kg quan trong no dung de chi muc
function ItemSearch($Keywords, $ItemPage){
$request="http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=".KEYID."&AssociateTag=".AssocTag."&Version=2006-09-11&Operation=ItemSearch&ResponseGroup=Medium,Offers&SearchIndex=Books";//&SearchIndex co cac gia tri nhu All, Apparel, Automotive, Baby, Beauty, Blended, Books, Classical, DVD, DigitalMusic, Electronics, GourmetFood, Grocery, HealthPersonalCare, HomeGarden, Industrial, Jewelry, Kitchen, Magazines, Marketplace, Merchants, Miscellaneous, Music, MusicTracks, MusicalInstruments, OfficeProducts, OutdoorLiving, PCHardware, PetSupplies, Photo, Software, SportingGoods, Tools, Toys, VHS, Video, VideoGames, Wireless, WirelessAccessories. O day de cho de minh chi tim sach kg thoi
echo $request.="&Keywords=$Keywords&ItemPage=$ItemPage";
$response = file_get_contents($request);
$parsed_xml = simplexml_load_string($response);
printSearchResults($parsed_xml);
}
function printSearchResults($parsed_xml){
$numOfItems = $parsed_xml->Items->TotalResults;
$totalPages = $parsed_xml->Items->TotalPages;
$CartId = $_GET['CartId'];
$HMAC = urlencode($_GET['HMAC']);
print("<table>");
if($numOfItems>0){
foreach($parsed_xml->Items->Item as $current){
if(isset($current->Offers->Offer->OfferListing->OfferListingId)){ //only show items for which there is an offer
print("<tr><td><img src='".$current->MediumImage->URL."'></td>");
print("<td><font size='-1'><b>".$current->ItemAttributes->Title."</b>");
if(isset($current->ItemAttributes->Director)){
print("<br>Giam doc san xuat: ".$current->ItemAttributes->Director);
} elseif(isset($current->ItemAttributes->Author)) {
print("<br>Tac gia: ".$current->ItemAttributes->Author);
} elseif(isset($current->ItemAttributes->Artist)) {
print("<br>Nghe sy: ".$current->ItemAttributes->Artist);
}
print("<br>Gia : ".$current->Offers->Offer->OfferListing->Price->FormattedPrice);
$asin = $current->ASIN;
$details = "amazon_chercher.php?Action=SeeDetails&ASIN=$asin&CartId=$CartId&HMAC=$HMAC";
print("<br><a href=$details>See Details</a>");
print("<tr><td colspan=2> </td> </tr> ");
}
}
}else{
print("<center>Kg co ket qua nao duoc tim thay.</center>");
}
print("<tr><td align='left'>");
//allow for paging through results
if($_GET['ItemPage'] > 1 && $totalPages > 1){ //check to see if there are previous pages
$Keywords = urlencode($_GET['Keywords']);
$ItemPage = $_GET['ItemPage']-1;
$prevPage = "amazon_chercher.php?Action=Search&SearchIndex=Books&Keywords=$Keywords&ItemPage=$ItemPage&CartId=$CartId&HMAC=$HMAC";
print("<a href=$prevPage>Trang Truoc </a></td><td align='right'>");
}
if($_GET['ItemPage'] < $totalPages){ //check to see if there are more pages
$Keywords = urlencode($_GET['Keywords']);
$ItemPage = $_GET['ItemPage']+1;
$nextPage = "amazon_chercher.php?Action=Search&SearchIndex=Books&Keywords=$Keywords&ItemPage=$ItemPage&CartId=$CartId&HMAC=$HMAC";
print("<a href=$nextPage>Trang sau</a></td></tr>");
}
}
//-------------------------------------------------------------------------------------------------------
function ItemLookup($asin){
$request = "http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=".KEYID."&AssociateTag=".AssocTag."&Version=2006-09-11&Operation=ItemLookup&ItemId=$asin&ResponseGroup=Medium,Offers";
$response = file_get_contents($request);
$parsed_xml = simplexml_load_string($response);
printDetails($parsed_xml);
}
//-------------------------------------------------------------------------------------------------------
function printDetails($parsed_xml){
print("<table>");
print("<tr><td><img src='".$parsed_xml->Items->Item->LargeImage->URL."'></td>");
print("<td>".$parsed_xml->Items->Item->ItemAttributes->Title);
print("<br>".$parsed_xml->Items->Item->ItemAttributes->Author);
print("<br>".$parsed_xml->Items->Item->ItemAttributes->Binding);
print("<br>".$parsed_xml->Items->Item->ItemAttributes->NumberOfPages." pages");
print("<br><b>".$parsed_xml->Items->Item->Offers->Offer->OfferListing->Price->FormattedPrice."</b>");
print("</table>");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Document sans titre</title>
</head>
<body>
<form id="SearchTerms" name="SearchTerms" method="get" action="">
<label>Mettre les mots clé
<input name="Keywords" type="text" id="Keywords" />
</label>
<label>
<input type='submit'/>
<input type='hidden' name='Action' value='Search' />
<input type='hidden' name='ItemPage' value='1'>
<input type='hidden' name='CartId' value=$CartId>
<input type='hidden' name='HMAC' value=$HMAC>
</label>
</form>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="100%" height="103" valign="top">
<?php
if($_GET['Action'] == 'Search'){
if($_GET['Keywords'] == ''){
print("Nhap tu khoa ma ban muon tim.");
}else{
$keywords = urlencode($_GET['Keywords']);
ItemSearch($keywords, $_GET['ItemPage']);
}
}
if($_GET['Action'] == 'SeeDetails'){
ItemLookup($_GET['ASIN']);
}
?>
</td>
<td width="202" valign="top">
</td>
</tr>
</table>
</body>
</html>
Tác giả: Ngọc Bảo
Tác giả:NguyÅn Minh Tân

