WordPress Plugin für Amazon AWS im Shortcode-Stil

6. Februar 2012 - Amazon, wordpress

Wenn man keinen großen Webshop mit Amazon Produkten auf seiner Webseite eröffnen möchte, kann man sich hier ein Plugin downloaden, mit dem man einzelne Produkte anzeigen und mit dem Amazon AWS experimentieren kann.

English Summary for „WordPress Plugin für Amazon AWS im Shortcode-Stil“:

For your first steps with the Amazon AWS API and single product display on your website you may downlad here a tiny WordPress plugin in shortcode-style. The code is based on Ulrich Mierendorffs Code with slight modifications. It’s displayed below to make shure that there is no modification of your AssociateTag :-).

Man braucht

  1. eine Account bei Amazon
  2. einen AssociateTag für die Region (com,de, fr, …)
  3. eine public key
  4. einen secret Key

Für die Region de ist eine gute erste Adresse http://partnernet.amazon.de

<?php
/*
Plugin Name: shortcode_amazon Plugin
URI: http://www.it-in-a-box.com/
Description: Zeigt Amazon Produkte mit  mit shortcodes an
Author: Norbert Felgendreher Author
URI: http://www.it-in-a-box.com/
Version: 1.1 */

class ShortcodeAmazon {
// constructor:
function ShortcodeAmazon() {
if ( !function_exists('add_shortcode') ) return; // Register the shortcodes
add_shortcode('amazon', array(&$this, 'amazon_ausgeben_handler'));
}

function aws_signed_request($region, $params, $public_key, $private_key) {
/* Parameters: $region - the Amazon(r) region (ca,com,co.uk,de,fr,jp)
$params - an array of parameters, eg. array("Operation"="ItemLookup",
"ItemId"=>"B000X9FLKM", "ResponseGroup"=>"Small")
$public_key - your "Access Key ID"
$private_key - your "Secret Access Key"
*/

// some paramters
$method = "GET";
$host = "webservices.amazon.".$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"] = "2011-08-01";
// 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;
// do request
//$response = file_get_contents($request);
//***************************************************
$session = curl_init($request);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
//***********************************
if ($response === False)
{
return False;
}
else
{
// parse XML
$pxml = simplexml_load_string($response);
if ($pxml === False)
{
return False; // no xml
}
else
{
return $pxml;
}
}
}

function amazon_ausgeben_handler ($atts, $content=null) {

extract(shortcode_atts(
array('asin'   => ''),$atts));
if ($asin==false) return;
$params=array("Operation"=>"ItemLookup", "ItemId"=>$asin, "ResponseGroup"=>"Large","AssociateTag"=>"your Amazon associate Tag");
$public_key='your public Amazon IDKey';
$private_key='your secret Amazon Key';
$region="de";
$xml_ret=$this->aws_signed_request($region, $params, $public_key, $private_key);
if ($xml_ret===False) return;
$myTitle = $xml_ret->Items->Item->ItemAttributes->Title;
$myImage = $xml_ret->Items->Item->MediumImage->URL;
return;
}
}
// Start this plugin once all other plugins are fully loaded
add_action( 'plugins_loaded', create_function( '', 'global $ShortcodeAmazon; $ShortcodeAmazon = new ShortcodeAmazon();' ) );
?>

AssociateTag, Keys und die Region werden in die Zeilen 96-99 eingetragen.
Aufgerufen wird das Plugin so: [ amazon asin=B0051QVF7A ] (ohne die Blancs hinter und vor den eckigen Klammern).Die asin ist die amazon standard identification number

Und so sollte es dann aussehen:

Hier kann man das Plugin downloaden Download Plugin shortcode_amazon