Friday, April 27, 2007

How to add Amazon links to a Drupal page

Do you want a quickie way to add Amazon prices to your Drupal node pages? Here's a code snippet for you:

/* Amazon Start */

$amazon_url = "http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=1XFK01HK9NZWGPENWGG2&AssociateTag=dagameboyz01-20&Version=2006-09-11&Operation=ItemSearch&ResponseGroup=Images,Medium&SearchIndex=All&Keywords=";
$amazon_url .= urlencode($title." ".$channel_id);
$amazon_url .= "&ItemPage=1";

$response = file_get_contents($amazon_url);

if ($response === false) {
die('Request failed');
}

$phpobject = simplexml_load_string($response);

if ($phpobject === false) {
die('Parsing failed');
}

$found_amazon = 0;
foreach ($phpobject->Items->Item as $item) {
if (($found_amazon == 0) && (($item->ItemAttributes->Binding == "Video Game") || ($item->ItemAttributes->ProductGroup == "Video Games"))) {
?>

$found_amazon++;
print "".l("Buy ".$title,$item->DetailPageURL, array(),NULL,NULL,TRUE).""; ?>
- (starting at OfferSummary->LowestNewPrice->FormattedPrice; ?>)
}
}
?>

No comments: