PHP Code to Connect to Amazon API and Display Results by Keyword
This is the function that I built and used to connect to the Amazon API.
I can post parsexml.inc.php if someone actually reads this and uses the information.
thanks.
Here is the code:
<?php require_once("parsexml.inc.php");
function AmazonResults($keywords,$page=1){
// original by Mauricio Zuniga
// http://www.ClearSkyInnovations.com
$a = str_replace("%20", " ",$keywords);
if ($_GET['page']){
$page = $_GET['page'];
}
?>
<style type="text/css">
<!--
.style2 {color: #FFFFFF}
.style4 {font-size: 14px}
.style5 {
font-size: 10px;
font-weight: bold;
}
.style7 {font-size: xx-small}
.style8 {font-size: 9px}
-->
</style>
<h1 class="style4"><span style="font-family: Verdana, Arial, Helvetica, sans-serif;color:#FFFFFF"><span class="style29">Amazon Results</span></span><br />
</h1>
<?php
//include 'clsParseXML.php';
$c = 'nintendo wii';
if (!($keywords=="")) {
$c = $keywords ;
}
$b='http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=[ YOUR KEY HERE ]&AssociateTag=chaosexistenceco&Version=2006-09-11&Operation=ItemSearch&ResponseGroup=Medium,Offers&SearchIndex=All&Keywords='.urlencode($c).'&ItemPage='.$page;
$xmlparse = &new ParseXML;
$xml = $xmlparse->GetXMLTree($b);
//echo 'TOTAL RESULTS'.$xml['ITEMS']['TOTALRESULTS'];
//echo 'TOTAL RESULTS'.$xml['ITEMSEARCHRESPONSE']['ITEMS']['TOTALRESULTS'];
$totalresults = $xml['ITEMSEARCHRESPONSE'][0]['ITEMS']['0']['TOTALRESULTS'][0]['VALUE'];
$items = $xml['ITEMSEARCHRESPONSE'][0]['ITEMS']['0'];
//echo 'totalresults = '.$totalresults;
$numtodisplay = 10;
$numberofpages = $totalresults/$numtodisplay;
if ($totalresults < $numtodisplay) {
$numtodisplay = $totalresults;
}
?>
<style type="text/css">
<!--
.stylea1 {font-family: Verdana, Arial, Helvetica, sans-serif}
.stylea3 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; }
-->
</style>
<table><?php
$maxcols = 3;
$colcounter = 0;
for ($number = 0; $number < $numtodisplay; $number++){
//echo $items['ITEM'][$number];
$url = $items['ITEM'][$number]['DETAILPAGEURL'][0]['VALUE'];
$img = $items['ITEM'][$number]['MEDIUMIMAGE'][0]['URL'][0]['VALUE'];
$asin = $items['ITEM'][$number]['ASIN'][0]['VALUE'];
?><?php
//echo $items['ITEM'][$number]['SMALLIMAGE'][0]['VALUE'];
$brand = $items['ITEM'][$number]['ITEMATTRIBUTES'][0]['BRAND'][0]['VALUE'];
$title = $items['ITEM'][$number]['ITEMATTRIBUTES'][0]['TITLE'][0]['VALUE'];
//echo $title.$brand;
$desc = "";
if (strlen($items['ITEM'][$number]['ITEMATTRIBUTES'][0]['FEATURE'][0]['VALUE']) > 0) {
$desc = $desc. $items['ITEM'][$number]['ITEMATTRIBUTES'][0]['FEATURE'][0]['VALUE'].'<br />';
}
if (strlen($items['ITEM'][$number]['ITEMATTRIBUTES'][0]['FEATURE'][1]['VALUE']) > 0) {
$desc = $desc. $items['ITEM'][$number]['ITEMATTRIBUTES'][0]['FEATURE'][1]['VALUE'].'<br />';
}
if (strlen($items['ITEM'][$number]['ITEMATTRIBUTES'][0]['FEATURE'][2]['VALUE']) > 0) {
$desc = $desc. $items['ITEM'][$number]['ITEMATTRIBUTES'][0]['FEATURE'][2]['VALUE'].'<br />';
}
if (strlen($items['ITEM'][$number]['ITEMATTRIBUTES'][0]['FEATURE'][3]['VALUE']) > 0) {
$desc = $desc. $items['ITEM'][$number]['ITEMATTRIBUTES'][0]['FEATURE'][3]['VALUE'].'<br />';
}
if (strlen($items['ITEM'][$number]['ITEMATTRIBUTES'][0]['FEATURE'][4]['VALUE']) > 0) {
$desc = $desc. $items['ITEM'][$number]['ITEMATTRIBUTES'][0]['FEATURE'][4]['VALUE'].'<br />';
}
if (strlen($items['ITEM'][$number]['ITEMATTRIBUTES'][0]['FEATURE'][4]['VALUE']) > 0) {
$desc = $desc. $items['ITEM'][$number]['ITEMATTRIBUTES'][0]['FEATURE'][5]['VALUE'].'<br />';
}
//echo $desc;
$lowestprice = $items['ITEM'][$number]['OFFERSUMMARY'][0]['LOWESTNEWPRICE'][0]['FORMATTEDPRICE'][0]['VALUE'].'<br />';
//echo $lowestprice;
//echo "$number <br />";
$uid = getUID();
if ($colcounter ==0) {
?><tr><?php
}
$domain = ucwords(str_replace('www.','',strtolower($_SERVER["SERVER_NAME"])));
?>
<td width="224" valign="top"><!-- start col-->
<div id="'<?php">'>
<table width="32%" border="0" cellpadding="3" cellspacing="0" bordercolor="#E8ECD8" bgcolor="#E8ECD8">
<tr>
<td height="2" colspan="2" background="http://www.saveonatoz.com/images/greenbuttonbackground2.gif" class="stylea1 style7"><a href="">');" class="style5" style="color:#FFFFFF" onmouseover="Tip('Compare Side by Side/Bookmark Item')" title="Compare Results Side by Side or Bookmark for Later">+ My List</a> | <span class="stylea1"><a href="ComparePrices.php?keywords=<?php echo str_replace(">&searchtype=broad" class="stylea3" style="color:#FFFFFF"><strong>More Like This One</strong></a> </span></td>
</tr>
<tr>
<td height="70" valign="top" bgcolor="#FFFFFF" class="stylea1"><div align="center"><img src="http://www.saveonatoz.com/images/cart_add.png" /></div></td>
<td valign="top" bgcolor="#FFFFFF" class="stylea1"><span class="stylea3"><b><a href="">/default.php?next=<?php echo urlencode($url);?>&searchfor=<?php echo urlencode($title);?>" class="stylea3" target="_top">Look at <span id="">"><?PHP echo substr($title,0,50);?></span></a></b> </span><span class="stylea3"><br />
</span></td>
</tr>
<tr>
<td width="2" bgcolor="#FFFFFF" class="stylea1"></td>
<td height="150" valign="middle" bgcolor="#FFFFFF" class="stylea1"><div align="center"><a href="">/default.php?next=<?php echo urlencode($url);?>&searchfor=<?php echo urlencode($title);?>"><img width="100" onmouseover="this.width='200';" onmouseout="this.width='100';" border="0" src="<?php if ($img == " />"><br />
</a></div></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" class="stylea1"><div align="center"><a href="" keywords="<?php">&searchtype=broad"><strong><img src="http://www.saveonatoz.com/images/magnifier_zoom_in.png" border="0" /></strong></a></div></td>
<td bgcolor="#FFFFFF" class="stylea1"><a href="" resulttype="A1&uid="<?php">&title=<?php echo str_replace(" ","-",$title);?>" target="_top" class="stylea3"><strong>Item Details</strong></a></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" class="stylea1"> </td>
<td bgcolor="#FFFFFF" class="stylea1"><div align="right">
<table width="100%" border="0" cellspacing="8" cellpadding="0">
<tr>
<td width="80%"><div align="right"><strong><?php echo $lowestprice?> </strong></div></td>
<td width="20%"><div align="right"><strong><a class="style2" target="_top" href="">/default.php?next=<?php echo urlencode($url);?>&searchfor=<?php echo urlencode($title);?>" onmouseover="Tip('Purchase At The Sellers Website')"><img src="http://www.saveonatoz.com/images/proceed-to-checkout.gif" align="bottom" border="0" /></a></strong></div></td>
</tr>
</table>
</div></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" class="stylea1"> </td>
<td bgcolor="#FFFFFF" class="stylea1"><div align="left"><?php if (strlen($desc) > 300) {?><div onmouseover="Tip('<?php echo str_replace(">')"><?php echo substr($desc,0,299).'...'; ?></div><?php } else { echo $desc; }?></div></td>
</tr>
</table>
</div><!--html snip-->
</td>
<?PHP
if ($colcounter==($maxcols-1)){
$colcounter =-1;
?></tr><?php
}//if
$colcounter ++;
}//for
?></table><?php //echo floor($numberofpages) ;
if ($numberofpages > 5) {
$numberofpages = 5;
}
for ($number = 1; $number <= floor($numberofpages); $number++){
?><a href="" searchtype="AMAZON&page="<?php">&keywords=<?php echo str_replace(" ","--",$a);?>"><?php echo $number; ?></a> <?
}
//echo "<pre>";
//print_r($xml);
//echo "</pre>";
} //function
function showAmazonItem() {
// original by Mauricio Zuniga
// http://www.ClearSkyInnovations.com
//require_once("../../include/parsexml.inc.php");
//echo 'qwww';
$ACCESS_KEY='YOUR ACCESS KEY HERE';
$asin='B000MRAAJW';
if ($_GET['uid']) {
$asin=$_GET['uid'];
}
# Build URL to query based on ASIN and ACCESS_KEY
$url='http://webservices.amazon.com/onca/xml?Service=AWSECommerceService';
$url.="&AWSAccessKeyId=0RQPV9B42VF2TWHHNA82&AssociateTag=chaosexistenceco&";
$url.="&Operation=ItemLookup&IdType=ASIN&ItemId=$asin";
$url.='&ResponseGroup=Medium,OfferFull';
# Place the results into an XML string
// $xml= file_get_contents($url);
// echo $xml;
$xmlparse = NULL;
$xmlparse = &new ParseXML;
$xml = $xmlparse->GetXMLTree($url);
//echo ' in here ';
//echo "<pre>";
//@print_r($xml);
//echo "</pre>";
$asin_returned = $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ASIN'][0]['VALUE'];
$ean = $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['EAN'][0]['VALUE'];
$format = $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['FORMAT'][1]['VALUE'];
$label = $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['LABEL'][0]['VALUE'];
//echo 'label'.$label;
$author = $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['AUTHOR'][0]['VALUE'];
$ean = $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['EAN'][0]['VALUE'];
$isbn = $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['ISBN'][0]['VALUE'];
$manufacturer = $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['MANUFACTURER'][0]['VALUE'];
$languages= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['LANGUAGES'][0]['LANGUAGE'][0]['NAME'][0]['VALUE'];
$listprice = $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['LISTPRICE'][0]['AMOUNT'][0]['VALUE'];
$currency= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['LISTPRICE'][0]['CURRENCYCODE'][0]['VALUE'];
$formatted_price= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['LISTPRICE'][0]['FORMATTEDPRICE'][0]['VALUE'];
$product_group= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['PRODUCTGROUP'][0]['VALUE'];
$publisher= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['PUBLISHER'][0]['VALUE'];
$region_code= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['REGIONCODE'][0]['VALUE'];
$studio= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['STUDIO'][0]['VALUE'];
$title= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['TITLE'][0]['VALUE'];
$upc= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['UPC'][0]['VALUE'];
$lowestnewprice= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['OFFERSUMMARY'][0]['LOWESTNEWPRICE'][0]['FORMATTEDPRICE'][0]['VALUE'];
$lowestnewprice_currency= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['OFFERSUMMARY'][0]['LOWESTNEWPRICE'][0]['CURRENCYCODE'][0]['VALUE'];
$lowestnewprice_formatted= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['OFFERSUMMARY'][0]['LOWESTNEWPRICE'][0]['FORMATTEDPRICE'][0]['VALUE'];
$lowestusedprice= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['OFFERSUMMARY'][0]['LOWESTUSEDPRICE'][0]['FORMATTEDPRICE'][0]['VALUE'];
$lowestusedprice_currency= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['OFFERSUMMARY'][0]['LOWESTUSEDPRICE'][0]['CURRENCYCODE'][0]['VALUE'];
$lowestusedprice_formatted= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['OFFERSUMMARY'][0]['LOWESTUSEDPRICE'][0]['FORMATTEDPRICE'][0]['VALUE'];
$total_new = $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['OFFERSUMMARY'][0]['TOTALNEW'][0]['VALUE'];
$total_used = $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['OFFERSUMMARY'][0]['TOTALUSED'][0]['VALUE'];
$detail_page = $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['DETAILPAGEURL'][0]['VALUE'];
$sales_rank= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['SALESRANK'][0]['VALUE'];
$small_image_url= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['SMALLIMAGE'][0]['URL'][0]['VALUE'];
$medium_image_url= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['MEDIUMIMAGE'][0]['URL'][0]['VALUE'];
$large_image_url= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['LARGEIMAGE'][0]['URL'][0]['VALUE'];
$actor= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['ACTOR'][0]['VALUE'];
$AUDIENCERATING= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['AUDIENCERATING'][0]['VALUE'];
$BINDING= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['ITEMATTRIBUTES'][0]['BINDING'][0]['VALUE'];
$editorial_review_content= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['EDITORIALREVIEWS'][0]['EDITORIALREVIEW'][0]['CONTENT'][0]['VALUE'];
$desc= $xml['ITEMLOOKUPRESPONSE'][0]['ITEMS'][0]['ITEM'][0]['EDITORIALREVIEWS'][0]['EDITORIALREVIEW'][1]['CONTENT'][0]['VALUE'];
//echo 'medium'.$medium_image_url;
//echo 'small image'.$small_image_url;
$document_title = $title.' '.$label;
$domain = ucwords(str_replace('www.','',strtolower($_SERVER["SERVER_NAME"])));
# Use Simple XML to put ?><h1><?=$title.' - '.$label?></h1>
<table width="682" border="0" cellpadding="4">
<tr>
<td width="4%"><a href="">"><img border="0" src="" />" /></td>
<td width="96%" valign="top"><?php
//echo 'total new : '.$total_new."<br />";
echo 'Listed Price : '.$formatted_price.' ('.$currency.')'."<br />";
//echo 'Currency : '.$currency."<br />";
echo 'Sales Rank : '.$sales_rank."<br />";
echo '[New] Lowest Price : <b>'.$lowestnewprice."</b><br />";
echo '[Used] Lowest Price : <b>'.$lowestusedprice."</b><br />";
echo 'Product Group : '.$product_group."<br />";
//echo 'title'.$title."<br />";
//echo 'Listprice : '.$listprice."<br />";
if (!($actor == '')) {
echo 'Actor : '.$actor."<br />";
}
if (!($author== '')) {
echo 'Author : '.$author."<br />";
}
if (!($AUDIENCERATING== '')) {
echo 'Audience Rating : '.$AUDIENCERATING."<br />";
}
echo 'Binding : '.$BINDING."<br />";
if (!($upc== '')) {
echo 'UPC : '.$upc."<br />";
}
if (!($ean== '')) {
echo 'EAN : '.$ean."<br />";
}
echo 'ASIN :'.$asin_returned."<br />";
//echo '<br /><br />'.$desc;
?><br />
<a href="" next="<?php">"><img src="http://www.saveonatoz.com/images/proceed-to-checkout.gif" align="bottom" border="0" /></a></td>
</tr>
<tr>
<td colspan="2"><?php echo $desc; ?><br />
<a href="">/default.php?next=<?php echo $detail_page; ?>&searchfor=<?php echo urlencode($title);?>"><img src="http://www.saveonatoz.com/images/proceed-to-checkout.gif" align="bottom" border="0" /></a><br />
<?php echo '<br /><br />'.$editorial_review_content.'<br /><br />';
?></td>
</tr>
</table>
<?php
};
?>
You are free to use it as long as you leave my very small credit and link in there as you modify.
If you find this information useful please visit one of the sponsors or just let me know.
Thanks and have a great day!
Labels: amazon, api, code, Connections, Display, Keyword, php, Results
2 Comments:
what happened with the psycho chick?
Web designers can't code, so they copy paste code upon copy-pasted codes.
Post a Comment
<< Home