Amazon Associate Hook for Link Farm Evolution

This is an Amazon Associate hook I made real quick for Link Farm Evolution. Basically it calls the Amazon API and does a search for whatever key word you specify, then picks a random result to make a post out of. To use this in LFE simple add the tag [amazon(keywordhere)].

One thing to note is that you will need to apply for the Amazon Associates program. Also you will need access to the AmazonAPI. You will need to add your Amazon apikey, private key, and amazon associates tag at the bottom of this file.

*having issue with the syntax highlighting right now, just copy everything in the php tags, save as amazon.php and upload this to your “contentsources” folder within lfe*

<?php
class amazon_csv
{
	public $url;
	public $user;
	public $pass;
	public $apikey;
	public $apiprivate;
	public $apitag;

	/*----Set initial variables-----------------------------------------------------*/
	function __construct($apikey,$apiprivate,$apitag)
	{
		$this->apikey = $apikey;
		$this->apiprivate = $apiprivate;
		$this->apitag = $apitag;
	}

	/*---Perform item search---------------------------------------------------------------*/
	public function ItemSearch($term,$index = 'All',$page = 1)
	{
		$params = array(
						"AssociateTag="=>$this->apitag,
						"Operation"=>"ItemSearch",
						"Keywords"=>$term,
						"SearchIndex"=>$index,
						"ResponseGroup"=>"Large",
						"ItemPage"=>$page
						);

		//Generated an amazon secuirty token using aws_signed_request
		$request = $this->aws_signed_request('com', $params, $this->apikey, $this->apiprivate);
		$session = curl_init($request);
		curl_setopt($session, CURLOPT_HEADER, false);
		curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
		$response = curl_exec($session);
		curl_close($session);
		$parsed_xml = simplexml_load_string($response);
		return $this->ParseResults($parsed_xml,$term,$page);
	}

	/*---- Parse the results returned from an amazon search and return the post array ----------------------------*/
	public function ParseResults($parsed_xml,$term,$page)
	{
		//echo "starting parse <br/>";
		$numOfItems = $parsed_xml->Items->TotalResults;
		$totalPages = $parsed_xml->Items->TotalPages;
		if($numOfItems>0){
			$i = 1;
			foreach($parsed_xml->Items->Item as $current){
					$asin = $asin = $current->ASIN;
					$image = $current->MediumImage->URL;
					$temptitle = $current->ItemAttributes->Title;
					$price = $current->Offers->Offer->OfferListing->Price->FormattedPrice;
					$details = $current->EditorialReviews->EditorialReview->Content;
					$posttext = "<a href='http://www.amazon.com/gp/product/$asin?ie=UTF8&amp;tag=";
					$posttext .= $this->apitag;
					$posttext .= "&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=$asin'>";
					$posttext .="<img src='$image' alt='$title' /></a><br>";
					$posttext .= "<img style='border:none !important; margin:0px !important;' src='http://www.assoc-amazon.com/e/ir?t=";
					$posttext .= $this->api-tag;
					$posttext .= "&amp;l=as2&amp;o=1&amp;a=$asin' border='0' alt='' width='1' height='1' />";
					$posttext .= "<p>$details</p>";
					$posttext .= "<h3><a href='http://www.amazon.com/gp/product/$asin?ie=UTF8&tag=";
					$posttext .= $this->apitag;
					$posttext .= "&linkCode=as2&camp=1789&creative=9325&creativeASIN=$asin'>$price - Buy This Item at Amazon.com</a>";
					$posttext .= "<img src='http://www.assoc-amazon.com/e/ir?t=";
					$posttext .= $this->apitag;
					$posttext .= "&l=as2&o=1&a=$asin' width='1' height='1' border='0' alt='' style='border:none !important; margin:0px !important;' /></h3>";

					//get similar product links
					if(isset($current->SimilarProducts->SimilarProduct)){
						$posttext .= "<br/><h2>Related Items</h2>";
						$posttext .= "<ul>";
						foreach($current->SimilarProducts->SimilarProduct as $product){
							$prodasin = $product->ASIN;
							$prodtitle = $product->Title;
							$posttext .= "<li><a href='http://www.amazon.com/gp/product/$prodasin?ie=UTF8&tag=" .$this->apitag. 

"&linkCode=as2&camp=1789&creative=9325&creativeASIN=$prodasin'>$prodtitle</a><img src='http://www.assoc-amazon.com/e/ir?t=" .$this->apitag. "&l=as2&o=1&a=$prodasin' width='1' height='1' border='0' alt='' 

style='border:none !important; margin:0px !important;' /></li>";
						}
						$posttext .= "</ul>";
					}
					//$posts[0] = 'asin,image,title,price,details,posttext';
					$title = $temptitle[0];
					$title = utf8_decode($title);
					$content = utf8_decode($posttext);
					$tags = utf8_decode($tags);
					$details = str_replace(array("\r", "\n", "\t"), '', $details);
					$posttext = str_replace(array("\r", "\n", "\t"), '', $posttext);
					//$posts[$i] = "$asin,$image,$title,$price,$details,$posttext";
					$posts[$i] = "$posttext";
					$i++;
			}
			return $posts;
		}else{
			print("<span style='color:red;'>No matches found on page $page.</span><br/>");
		}
	}

	/*---This function generate secure amazon transactions. Fucking stupid -------------------------*/
	function aws_signed_request($region, $params, $public_key, $private_key)
	{
		// some paramters
		$method = "GET";
		$host = "ecs.amazonaws.".$region;
		$uri = "/onca/xml";

		// additional parameters
		$params["Service"] = "AWSECommerceService";
		$params["AWSAccessKeyId"] = $public_key;
		// GMT timestamp
		$params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z");
		// API version
		$params["Version"] = "2009-03-31";

		// sort the parameters
		ksort($params);

		// create the canonicalized query
		$canonicalized_query = array();
		foreach ($params as $param=>$value)
		{
			$param = str_replace("%7E", "~", rawurlencode($param));
			$value = str_replace("%7E", "~", rawurlencode($value));
			$canonicalized_query[] = $param."=".$value;
		}
		$canonicalized_query = implode("&", $canonicalized_query);

		// create the string to sign
		$string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query;

		// calculate HMAC with SHA256 and base64-encoding
		$signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));

		// encode the signature for the request
		$signature = str_replace("%7E", "~", rawurlencode($signature));

		// create request
		$request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature;

		return $request;
	}
}

function amazon($keyword = '') {
	//run and tell dat
	$apikey = 'YOUR-API-KEY-HERE';
	$apiprivate = 'YOUR-API-PRIVATE';
	$apitag = 'YOUR-AMAZON-ASSOCIATE-TAG';
	$wp = new amazon_csv($apikey,$apiprivate,$apitag);
        $posts = $wp->ItemSearch($keyword);

	//pick a random result
	$rand_keys = array_rand($posts, 2);
	echo $posts[$rand_keys[0]] . "\n";
 }
?>

One Response to “Amazon Associate Hook for Link Farm Evolution”

  1. kaostyl says:

    Hello,
    i would like to adapt your script for french products.
    Could you please tell me how to do that?
    Thanks for your help

Leave a Reply