Thursday, October 19, 2006

Amazon API IntelliTXT

I'm sure other people have done this, but I created a script that injects Amazon products (w/ new product previews) into articles similar to the way IntelliTXT works. I use the automatic keyword class from previous posts to find keywords in an article, search the Amazon API with those keywords, and then replace the keywords in the article with the newly created link. It seems to work fairly well. Mouse over the links in this post to see how the Amazon Previews work.


//To use this you'll need to first download tools.inc.php
//which is included with the AWS Book (awsbook.com)
//samples.
//http://sourceforge.net/project/showfiles.php?group_id=88612
//Find tools.inc.php under 'Chapter 4'
//
require_once("tools.inc.php");
define('SUBID', '1A7XKHR5BYD0WPJVQEG2'); // Your subscription id

define('Searchindex','Books');
//define('Searchindex','Music');
//define('Searchindex','DVD');
//define('Searchindex','Toys');
//define('Searchindex','Video Games');
//define('Searchindex','Software');
//define('Searchindex','Software Video Games');
//define('Searchindex','Electronics');
//define('Searchindex','Tools');
//define('Searchindex','Sporting Goods');
//define('Searchindex','Art Supplies');
//define('Searchindex','Kitchen');
//define('Searchindex','Gourmet Food');
//define('Searchindex','Apparel');
//define('Searchindex','PC Hardware');
//define('Searchindex','VHS');

function awsreplace($keyword) {
$request = 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId='.SUBID.'&Operation=ItemSearch&Keywords='.urlencode($keyword).'&SearchIndex='.Searchindex.'&Sort=salesrank';
// Get the response from Amazon
$xml = file_get_contents($request);
// Parse the results
$Result = xmlparser($xml);
$ASIN = $Result['ItemSearchResponse']['Items'][0]['Item'][0]["ASIN"][0];
$url = '<a href="http://www.amazon.com/gp/product/'.$ASIN.'?ie=UTF8&tag=duelcoastweb-20&linkCode=as2&camp=1789&creative=9325&creativeASIN='.$ASIN.'">';
$replacestr = $url . $keyword . '</a>';
if (!isset($Result['ItemSearchResponse']['Items'][0]['Item'][0]["ASIN"][0])) {
$replacestr = $keyword;
}
return $replacestr;
}


I call the function from within my page like this:

foreach ($keyword_array AS $keyword_replace) {
$body = str_ireplace(trim($keyword_replace),awsreplace(trim($keyword_replace)),$body);
}
?>

No comments: