April 3, 2009

AssociateTag for Zend_Service_Amazon

I've been using the Zend Framework recently.
Zend_Service_Amazon did not have an option to configure your AssociateTag, which is your identifier from Amazon's Associate Web Services.

So I added that to the Zend Service, now all returned links contain your tag.

To use it, just call the constructor with your AssociateTag:

$amazon = new Zend_Service_Amazon_Query($awsId, $countryCode, $associateTag);

Here are the changes to Amazon.php:

class Amazon {
 ...
public $associateTag;

public function __construct($appId, $countryCode = 'US', $associateTag = '')
{
...
$this->associateTag = (string) $associateTag;
...
 }

public function itemSearch(array $options)
{
$defaultOptions = array('ResponseGroup' => 'Small', 'AssociateTag' => $this->associateTag);
 ...
 }

public function itemLookup($asin, array $options = array())
{
$defaultOptions = array('IdType' => 'ASIN', 'ResponseGroup' => 'Small', 'AssociateTag' => $this->associateTag);
 ...
 }
That's it! Easy, right?

Suggestions are welcome!
Tell me if it works for you and what you use it for. :)

2 comments:

rchouinard said...

I'm not familiar with the Zend_Service_Amazon components, but I really would object to modifying the components directly instead of extending them in your own library space. Modifying the library directly limits your ability to perform drop-in upgrades, and violates OO principles.

Daniel Contag said...

rchouinard:
I agree in principle but this enhancement is more of a core functionality in my opinion. I was rather surprised that it was not included in the framework. Tarzan AWS for example has this configuration option.