I've created a couple of AWS enabled plugins for Serendipity before, but when Amazon started requiring Signed Request, I never got around to updating them. When I did, I had all kinds of hell making the standard HTTPS post work.
I decided to try my hand at converting it over to soap, which was a lot quicker and easier than I thought. I created a customized amazon search page using my newfound soap skills. Keep reading to see the rest of the code.
<html> <head> <link rel="stylesheet" type="text/css" href="http://dustinhawkins.com/cms/index.php?/serendipity.css" /> </head> <body> <center> <img src='http://dustinhawkins.com/cms/templates/dustinhawkins/img/logo.png'><br><img src='/img/amazon_logo.gif'><br><br> <form action='' method=POST> <input type='text' name='item' size='100'> <input type='submit' name='sub'> </form> </center> <br><BR><BR> <?php if (isset($_REQUEST['sub'])) { print ("<hr width='80%'>"); $response = find_match ($_REQUEST['item'], 'All'); //print_r($response); if (is_array($response)) { foreach ($response as $item) { print (" <div > <table cellspacing=4 cellpadding=4 align='left' width='500px'><tr><td align='left' width='1%'> <a href='$item[Detail]'><img src='$item[Image]'></a> </td><td align='left'> <a href='$item[Detail]'>$item[Title]</a><br> <b>$item[Price]</b>: $item[Rating]/5.0 - $item[Reviews] reviews </td> </tr></table> </div>
"); } } }//Find Matches - Assume we're in the US, and searching ALL sections of amazon.function find_match ($search,$section="All",$locale='US'){ $id='<Your Amazon Access Key ID>'; $key='<Your Amazon Secret Access Key>'; $wsdl = "http://ecs.amazonaws.com/AWSECommerceService/2009-10-01/$locale/AWSECommerceService.wsdl"; $associates_tag = '<Your Amazon Associates Tag'; // Create the DigSig for the AWS Call. $timeStamp = gmdate("Y-m-d\TH:i:s\Z"); $string = 'ItemSearch'.$timeStamp; $signature = base64_encode(hash_hmac("sha256", $string, $key, True)); $client = new SoapClient ($wsdl);// Uncomment the following to see all the SoapFunctions & Types.// var_dump($client->__getFunctions());// var_dump($client->__getTypes());// var_dump($client); //Create search params $params = array ( "Service"=>"AWSECommerceService", "AssociateTag"=>$associates_tag, "AWSAccessKeyId"=>$id, "Timestamp"=>$timeStamp, "Signature"=>$signature, "Operation"=>"ItemSearch", "Keywords"=>$search, "SearchIndex"=>"All", "ResponseGroup"=>"Large", ); //Map our array to the appropriate AWS Soap Object. $itemSearch = new SoapVar ($params, SOAP_ENC_OBJECT ,'ItemSearch',$wsdl); //Run the Search $response = $client->ItemSearch($itemSearch); //Uncomment to see the entire response object //print ("<pre>"); //print_r($response); //print ("</pre>"); //If its an array, we have multiple items. If its an object, its a single object. //Create a slimmed down array from the reponse w/ the stuff I want in it. if (is_array($response->Items->Item) ) { foreach ($response->Items->Item as $item) { $toReturn[] = array( "Image"=>$item->MediumImage->URL, "Height"=>$item->MediumImage->Height, "Width"=>$item->MediumImage->Width, "Detail"=>$item->DetailPageURL, "Title"=>$item->ItemAttributes->Title, "Rating"=>$item->CustomerReviews->AverageRating, "Reviews"=>$item->CustomerReviews->TotalReviews, "Price"=>$item->OfferSummary->LowestNewPrice->FormattedPrice ); } return ($toReturn); }elseif (is_object($response->Items->Item) ) { $item=$response->Items->Item; $toReturn[] = array( "Image"=>$item->MediumImage->URL, "Height"=>$item->MediumImage->Height, "Width"=>$item->MediumImage->Width, "Detail"=>$item->DetailPageURL, "Title"=>$item->ItemAttributes->Title, "Rating"=>$item->CustomerReviews->AverageRating, "Reviews"=>$item->CustomerReviews->TotalReviews, "Price"=>$item->OfferSummary->LowestNewPrice->FormattedPrice ); return($toReturn); }else { return(false); }}?></body> </html>
|