We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

New Module : CJM_Amazon allowing Amazon Associate Web Service data fetching

Development and Programming

Chris MacPherson's avatar
Chris MacPherson
9 posts
16 years ago
Chris MacPherson's avatar Chris MacPherson

Hi,

I have just created a module that allows you to fetch items from Amazon using their Associate Web Service. I needed one for my site so I built one, maybe you will find it useful also. It’s PHP5 only at the moment I’m afraid, but it will work with either “allow_url_fopen” switched on in the php.ini file or by having Curl installed.

Some features are:

  • Control panel to set default Associate Id, Associate tag, and Locale.
  • Listing of errors in control panel to show any problems with setup or item related data.
  • Choice of using Medium or Small ‘Response groups’ which can mean less data is fetched from Amazon for efficiency.

Example usage:

Here is a simple example fetching the title of a book.

{exp:cjm_amazon:item asin="1857231384"}
    {Title}
{/exp:cjm_amazon:item}

In this example we want to display the product image as well so we need the Medium response group which contains the image URL details.

{exp:cjm_amazon:item asin="1857231384" response_group="Medium"}
    {Title}
    {SmallImageURL}
{/exp:cjm_amazon:item}

If this book was only available in the UK then I’d also add the Locale.

{exp:cjm_amazon:item asin="1857231384" response_group="Medium" locale=".co.uk"}
    {Title}
    {SmallImageURL}
{/exp:cjm_amazon:item}

Download it here : CJM_Amazon

       
David Webb's avatar
David Webb
62 posts
16 years ago
David Webb's avatar David Webb

Awesome. I was just about to try and build something like this myself when my laptop died on me last week. So thanks for releasing it.

I’ll give it a test when my laptops back with me.

David

       
Jack McDade's avatar
Jack McDade
425 posts
16 years ago
Jack McDade's avatar Jack McDade

Just tried this out, it works perfectly! Now to figure out how to loop through a big list of books…

       
Chris MacPherson's avatar
Chris MacPherson
9 posts
16 years ago
Chris MacPherson's avatar Chris MacPherson

Cool, glad it’s working for you. Since I posted this new module I have found out that when linking to items in different locales you actually need to have a seperate affiliate account with that specific locale as well,in order to get paid that is!

I will be updating the module to include the ability to save mutiple affiliate accounts which are used according to the item locale. This will be at least a month away though as just moved house and have no internet. Will post updates here though.

Happy Christmas! (Good time for affiliate links!!)

       
Jack McDade's avatar
Jack McDade
425 posts
16 years ago
Jack McDade's avatar Jack McDade

Awesome, thanks Chris! So far so good in my implementation, just about done. You may want to think about adding a prefix to the {Author} tag in case you use the {exp:cjm_amazon:item} tag inside {exp:weblog:entries}. If you miss the Uppercase you get the logged in “author”. Other than that, awesome job.

Merry Christmas!

       
Jack McDade's avatar
Jack McDade
425 posts
16 years ago
Jack McDade's avatar Jack McDade

Oh hey, one other question. How difficult would it be to build in some caching functionality? We could look into it if we knew what variables the data come in on. Any insight you might have would be helpful. Thanks!

       
Linda A's avatar
Linda A
647 posts
about 16 years ago
Linda A's avatar Linda A

Just found this thread, and I was wondering if this module supports the authenticated calls that now have to be used when fetching data from Amazon? I’ve been using an old, no longer supported module for years, and it no longer works after that changed was introduced.

I was also wondering if the module was ever updated to allow for associate ids for two different locales?

       
Chris MacPherson's avatar
Chris MacPherson
9 posts
about 16 years ago
Chris MacPherson's avatar Chris MacPherson

Hi,

Currently the module does not have either of the features you mention. I am planning on updating it this week/weekend though. I had another enquiry the other day asking a similar question too.

I will post here again once I’ve completed the update.

Cheers,

Chris

       
jasonkruit's avatar
jasonkruit
8 posts
about 16 years ago
jasonkruit's avatar jasonkruit

I have been using the module and it works great. I noticed today that it isn’t pulling in any info from Amazon. I see it is broken on your website also. Could you point me in the correct direction to troubleshoot my problem. Thanks!

       
jasonkruit's avatar
jasonkruit
8 posts
15 years ago
jasonkruit's avatar jasonkruit

I have it creating a signed request now. I was wondering if you could explain how you are pulling in the associate info. I would like to have that pull in my public and private keys instead. I have the solution hard coded right now and would like to bring in those values dynamically. Thanks!

       
Linda A's avatar
Linda A
647 posts
15 years ago
Linda A's avatar Linda A

I believe it would be broken right now because Amazon started disallowing unsigned API requests mid-August, so it won’t work again until updated for the new request requirements. 😊

       
jasonkruit's avatar
jasonkruit
8 posts
15 years ago
jasonkruit's avatar jasonkruit

I have it working. I added a function that creates the signed request. I have hard coded my public and private keys into the solution.

I would like to see if I could put those keys into the space in the DB where the associate info now resides. My thinking was that since the associate info is no longer needed I could use Chris’ calls to fetch the keys instead. I haven’t gone through the whole module line by line to see what is actually being done.

Hope that helps clarify my last ask of Chris.

Thanks for the input! The module works great! I like how easy it was to get it working.

       
Chris Jennings's avatar
Chris Jennings
87 posts
15 years ago
Chris Jennings's avatar Chris Jennings

jasonkruit -

I have it working. I added a function that creates the signed request. I have hard coded my public and private keys into the solution

could you explain how you coded in the public and private keys and how you obtained them from AWS? What url are you sending the request to? And what do the parameters look like?

I am really struggling to understand this. The documentation and sample code at the Amazon site seems a bit confusing. I already have the Associate tag and the AWSAccessKeyId..

       
jasonkruit's avatar
jasonkruit
8 posts
15 years ago
jasonkruit's avatar jasonkruit

Your Access Key ID & Secret Access Key can be found in the menu under “Your Account->Security Credentials” on the AWS site.

Here is the function I used to sign the request.

function  aws_signed_request($region,$params,$public_key,$private_key)
    {

    $method = "GET";
    $host = "ecs.amazonaws".$region; // must be in small case
    $uri = "/onca/xml";
    
    
    $params["Service"]          = "AWSECommerceService";
    $params["AWSAccessKeyId"]   = $public_key;
    $params["Timestamp"]        = gmdate("Y-m-d\TH:i:s\Z");
    $params["Version"]          = "2009-03-31";

    /* The params need to be sorted by the key, as Amazon does this at
      their end and then generates the hash of the same. If the params
      are not in order then the generated hash will be different thus
      failing the authetication process.
    */
    ksort($params);
    
    $canonicalized_query = array();

    foreach ($params as $param=>$value)
    {
        $param = str_replace("~", "~", rawurlencode($param));
        $value = str_replace("~", "~", rawurlencode($value));
        $canonicalized_query[] = $param."=".$value;
    }
    
    $canonicalized_query = implode("&", $canonicalized_query);

    $string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query;
    
    /* calculate the signature using HMAC with SHA256 and base64-encoding.
       The 'hash_hmac' function is only available from PHP 5 >= 5.1.2.
    */
    $signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));
    
    /* encode the signature for the request */
    $signature = str_replace("~", "~", rawurlencode($signature));
    
    /* create request */
    $request = "http://".$host.$uri."?".$canonicalized_query."&Signature;=".$signature;
        return($request);
       }

Here is the section of code I modified on the module. Lines 102-115

// Only proceed with valid ASIN, access_key_id and associate_tag
        if ($asin != '' && $this->access_key_id != '' && $this->associate_tag != '')
        {

            // Set the values for some of the parameters.
            $params["ItemId"]= $asin;
            $params["Operation"] = "ItemLookup";
            $params["ResponseGroup"] = "Medium";
            $region             = $locale;
            $public_key         = "Access Key ID Goes Here";
            $private_key     = "Secret Access Key Goes Here";
            
            // Define the request
            $request = $this->aws_signed_request($region,$params,$public_key,$private_key);
               
            // Fetch the request and convert to XML

I don’t pass any of the associate info that the module stores in the database to the request to Amazon. I hope this helps. It is not pretty but it is working for now. I am sure I will revisit this later and clean up the code. I want to move the keys out of this file.

       
Chris Jennings's avatar
Chris Jennings
87 posts
15 years ago
Chris Jennings's avatar Chris Jennings

Brilliant! Thanks for the bits of code. Actually I am not using the Chris MacPherson module but rather a plugin patched in from earlier plugins. The Chris MacPherson module link seems dead and I don’t have.

Anyway, I am happy with what I have now, so thanks again…. here is a page: http://www.pagetoscreen.net/bookshelf/

Chris

       
1 2

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.