The Glass is Too Big - Home

Amazon Wishlists with PHP

Originally published on: 10/13/2006 7:11:35 PM

As with my music attention, I've been wanting a relatively easy way to share my book reading attention as well as other purchases. Given how I pretty much buy all of my books from Amazon and how much other stuff I buy from them, their wishlists seemed a reasonable way to semi-passively share a reading list, etc. When I buy a book, I can just also add it to a specific list and have that ripple out onto pages on this site.

The first thing to do is get a quick handle on all of your wishlists that Amazon has on file. The scratchpad for the ListSearch gives a handy way to build the URL. Set the ListType to WishList and the rest is fairly straightforward. Include the email and the subscription ID (get yours at http://aws.amazon.com).

I gave it my email address and got my URL, which I then ran through the following code.



$xml = simplexml_load_file($url);

$lists = $xml->Lists;

foreach($lists->List as $list){

$ListURL = $list->ListURL;

$ListName = $list->ListName;

$TotalItems = $list->TotalItems;

$ListId = $list->ListId;

print("$ListName ($ListId) has $TotalItems items<br />");

}



That's useful if you want to find lists, for example to find the lists of family members, etc. However, for the next bit, I only really needed the $ListId, but who knows, the above might be useful and it got me the bit I needed.

So, we can do something very similar to get the URL to retrieve a list. The scratchpad for ListLookup gets the URL we need.

Again, using thhe SimpleXML functions is the quickest way to grab the data. Load your URL into a variable (it's a really long URL, so I'm not posting it to avoid messing the layout up). Anyway, when you run:



$xml = simplexml_load_file($url);



You have this object that represents what's in the XML. As such, it pretty much varies based on the XML you load. The easiest way to see what's inside is to do a print_r($xml) and you'll see a nicely nested object in the view source. Using the <pre> tag can make looking at the output easier.

In the case of a ListLookup, using this lookup gives you the actual details of the list:



$list = $xml->Lists->List;



Another quick print_r($list) and we can see that we're getting to the meat of what we're after. Depending on what properties you selected to build your URL, you may or may not have all of these, but for books and reasonable options, this little snippet:



$xml = simplexml_load_file($url);

$list = $xml->Lists->List;

foreach($list->ListItem as $item){

$detailURL = $item->Item->DetailPageURL;

$title = $item->Item->ItemAttributes->Title;

$author = $item->Item->ItemAttributes->Author;

print("$title by $author<br>");

}



Which generates a nice little list like this:

Stumbling on Happiness by Daniel Gilbert
The Long Tail: Why the Future of Business Is Selling Less of More by Chris Anderson
The Wisdom of Crowds by James Surowiecki
The Paradox of Choice: Why More Is Less (P.S.) by Barry Schwartz
Blink: The Power of Thinking Without Thinking by Malcolm Gladwell


If you did much digging through the object that you get from Amazon, you saw just how much data there is, which would make for whatever kinds of customized pages you want.

Comments

mo
commented on 2/24/2009
hi ..

nice tutorial ..

just a question :

now, when you have your links listed, if someone wants to buy you something from the list, he clicks on the link, but there is no option to buy it for you on amazon !!? so how you will get the item, as no one will see your address, all they'll see is the article description and the ability to buy it for themselves, not you !!! right ?

how can we solve this !?

thanks in advance
blog comments powered by Disqus
© 2003- 2010 J Wynia. Very Few Rights Reserved.