PHP Anaconda: Amazon AWS 4.0 keyword search script
UPDATE, 10/24/09: PHP Anaconda has served its purpose but in August of this year Amazon started requiring that all API requests be signed, rendering the Amazon e-commerce script useless. While it no longer works right out of the box, you can try integrating mierendo.com’s Amazon® AWS HMAC signed request script to retain the simplicity and easy deployment of PHP Anaconda.
If you’re looking for a quick and simple implementation of the Amazon Associates Web Service for your website, stick around and learn about how PHP Anaconda came to be.
On a number of my websites, I was using the old Amazon ECS web service to pull in a handful a related products based on a keyword search. I used a hacked up version of the Filzhut.de amazon_functions scripts. When March 31st of this year rolled around however, Amazon shut down the ECS service in favor the newer Associates Web Service (AWS). This was no surprise: Amazon AWS 4.0 was released back in mid-2005 (if memory serves me correctly), though it was a little buggy at first. Flash forward to 2008 and it was clear that I would have to come up with a new way to display Amazon products on my websites.
My needs were straightforward:
- As simple as possible: not too many files, called with a simple include statement
- Support for both PHP 4 and PHP 5
- No dependencies on PEAR packages
- Keyword search driven
- Random results
- Caching
I assumed it would be easy enough to hit up Google and find a prefab PHP microapp out there that would do the trick. Unfortunately, most of the stuff I found was not compatible with AWS 4.0 or was PHP 5 only.
No worries, I thought, I’ll just put Paul Reinheimer’s Professional Web APIs with PHP book to good use. Using his scripts and guidance, I pasted together some code that would satisfy my needs, tested it out, and started to deploy it. Then on March 31, I noticed none of it was working! As it turns out, the code in Paul’s book, published in July 2006—after Amazon AWS 4.0 had been released—was not AWS 4.0 compatible! After some forehead slapping, I leafed through the book and noticed that it didn’t say anywhere what version of Amazon’s web service the code was compatible with. Given Amazon’s multiple offerings, that seemed like an oversight. I visited Paul’s website, the Wrox forums, the author’s notes on Amazon.com, but nowhere was there any mention of AWS 4.0-compatible code available. It was starting to look like I’d have to put a little more elbow grease into this.
I’m not one to reinvent the wheel though, so I hit up my bookshelf again and grabbed some code from Brett O’Connor’s del.icio.us Mashups that would handle making the REST request to Amazon and process the results using the Snoopy library. Next, I pasted in Simon Willison’s simple Instant Caching with PHP code for the caching mechanism. I was still having trouble making sense of the very deeply nested arrays in Amazon’s response however, despite this using this handy function to make the output more manageable. In the end, I got some help from Christian Kamau, who created the code to output those tricky arrays into clean lists and randomize the output.
The result is PHP Anaconda, a simple Amazon AWS keyword search script that accesses the web service via REST and returns either three random results or the first ten results. There’s nothing mind blowing here, it just does the job I needed it to, and since I’m sure I’m not the only one who wants this functionality, I’ve made it available for download:
There’s a lot you can do with this code if you’re willing to dig a little deeper. For those adventures I recommend the Amazon AWS Developer Guide. Have fun navigating the Rio Amazon, and watch out for piranhas.