Personal Store


Amazon's wide range of products and high brand recognition combined with revenue potential has prompted many individuals and firms to advertise selected Amazon products through their own websites. Although the examples presented earlier allow users to search through Amazon's entire catalog, that may not be what you are looking for. By creating a miniature "personal store," you can present selected items to your visitors, and still earn a percentage of any sales generated.

There are four methods of choosing products to display to your visitors that are programmatically significant (by that, I mean four ways to select the products from Amazon's catalog, not four ways for you to choose which products to display). First, you could maintain a list of desired ASIN numbers in a local database, then retrieve all or a portion of that list, populate the information from Amazon, and display it to the end user. Second, you could select products from Amazon based on some common criteria that Amazon allows you to search by, and query the database in that manner. Third, you could create a list (Listmania is the terminology used on Amazon's site) via Amazon's regular interface. Finally, you could select products indiscriminately based on keywords associated with your site.

Maintaining a list of desired ASINs in a local database is a rather simple but elegant solution. The values are sought and recorded locally. These items are then displayed (generally a random selection of the elements is displayed) to the end user, possibly depending on which page in particular they are visiting within a site. Implementing this solution should be simple using the code discussed earlier in the chapter; as such, the code is not presented here (it is available online with the code from this book).

There are two things to keep in mind with this approach. First, you may want to retrieve more ASIN values from the database than you will actually need, so products that are not available (either the result is returned and the Availability value is set to not available, or you are unable to get a result for that item) can be skipped. Second, caching will likely be really tempting, but make sure you read the Amazon web services Terms of Service carefully. There are requirements on how long you can hold information that has been removed from Amazon's database.

The method of selecting products via common criteria should also be rather easy programmatically based on earlier examples; as such, the code is available online if needed. The author search example can be easily modified to find all products by a given manufacturer or publisher, both of which have custom search functions on Amazon. Individual products may then be selected for display; you may want to use price or availability as a criterion in this selection.

The method of creating a list of items on Amazon is both attractive and programmatically interesting. I find this approach appealing because it allows me to move the job of selecting which products to display to a third party, which is presumably not a programmer but someone who is better suited to the job. They can then use the Amazon interface that they are already (if I may presume) familiar with to create and maintain a list of desirable products. This is a great zero-maintenance solution for programs to be given to third parties, because they can keep their lists fresh and you don't need to intervene or educate new users on how to add new items to the list. This approach is discussed in depth later in the chapter.

Selecting random products via some keywords is an attractive approach for sites that garner a significant number of visitors. With a large number of visitors, you must select a lot of products in order for the offerings to remain fresh — there is little point in offering four of the same ten products to visitors 50 times. If they didn't click the first time, they won't click the next 20 times. This method is also discussed in depth later.

Using Amazon's List to Populate a Personal Store

Before there is any confusion on the issue, the list discussed here is not a wish list, but instead a list usually shown under Listmania! Selections made from wish lists are by default shipped to the creator of the list, then removed from the list itself, neither of which are the desired action in this case. What you are trying to do is present products that you feel the buyer would like for themselves, so they should be shipped to the user, and should remain on the list for other visitors to see.

Creating a list on Amazon for the first time might be a bit tricky. First, the user selecting the items will need an account (this does not need to be attached in any way to the developer account or associate account), which should be easy (go to amazon.com, click on Your Account, and register). The next step is the tricky one, the option to create a list under Listmania! doesn't appear anywhere under the Your Account menu. Browse on Amazon product, then scroll down. Listmania! should either appear on the left sidebar or in the central column; if not, choose another product.

Once you have found the Listmania! listing, click on a list, and at that point you will have the opportunity to create your own list (there should be a link in the top-right corner of the screen). Re-enter your password and select some privacy options, and you can begin working on your list. You will need a title and the ASINs for the items in question. The ASIN for a product should be listed under Product Details, near the bottom of that list. You can also enter a description of each item added to the list.

Note the List ID. You can determine this by looking at the URL shown when you examine the list itself:

 http://www.amazon.com/exec/obidos/tg/listmania/list-browse/-/3OI5ZK8PHO3WG/103- 3019150-3420664 

The List ID is the 13-character string shown after /list-browse/-/, which is 3OI5ZK8PHO3WG in this example. All other information can be ignored; Amazon likes to keep lots of information in their URLs.

Next, plug that ID into a new query function:

 function runListManiaQuery($client, $type = 'lite') {   $params = array(                     'lm_id'       => '3OI5ZK8PHO3WG',                     'type'        => $type,                     'page'        => $page,                     'tag'         => 'preinheimerco-20',                     'devtag'      => '1PHH5VTRY7D300H7JTR2'   );   $namespace = 'http://soap.amazon.com';   $action = 'http://soap.amazon.com';   $method = "ListManiaSearchRequest";   $result = $client->call($method,   array('ListManiaSearchRequest' => $params),   $namespace, $action);   return $result; } 

This function is again very similar to the ones presented earlier, but it does allow you to select exactly which items are returned. Using that function, and with a little effort to make the results attractive, you can end up with My Amazon List shown as a sidebar for my blog in Figure 7-6.

image from book
Figure 7-6

Using Keywords to Populate Your Personal Store

Using keywords relevant to your site to select random products to display to your visitors is an easy way to ensure that users are constantly presented with fresh, relevant content. You can reuse the runSearchQuery() function presented toward the beginning of the chapter, and with a small addition, present random products to your users. Basically, what I'm suggesting is that you determine a series of keywords relating to your site. For example, a community site for people who knit might use keywords like knitting, yarn, and patterns.

Conceptually, the code to select the random elements is pretty simple. First, select a keyword from the hard-coded array and run the query. Next, select and output the required number of elements. I have used some rather fun code to avoid checking for duplication or manually generating random numbers. I have left out the error checking code (it's identical to other examples) for the sake of brevity.

 <?php $keywords = array(); $keywords[] = "php"; $keywords[] = "mysql"; $keywords[] = "apache"; $keywords[] = "linux"; $keyword = $keywords[array_rand($keywords)]; 

Here the desired keywords are defined, and one is selected at random. Using PHP's built-in array functions rather than manually generating a random number based on the length of the array helps keep your code short and fast. Note that array_rand() returns the index of the selected element, not the element itself, which is why it is encapsulated in $keywords[].

 require('../lib/nusoap.php'); $client = new soapclient("http://soap.amazon.com/schemas2/AmazonWebServices.wsdl", true); $result = runSearchQuery($client, $keyword, '1', 'books', 'lite'); $error = $client->getError(); $resultItems = $result['Details']; 

This code should look quite familiar by now.

 for ($i = 0; $i < 4; $i++) {     $selection = array_rand($resultItems);     $item = $resultItems[$selection];     array_splice($resultItems, $selection, 1); 

Again the array_rand() function is used to select an element from the array. This time, however, you can't directly access that element, because you need to know which one was selected. Using that selection value and array_splice(), you remove the element from the array to ensure it is not selected in a future run.

     $title = $item['ProductName'];     $url = $item['Url'];     $image = $item['ImageUrlSmall'];     $authorList = @implode($item['Authors'], ", ");     $price = $item['ListPrice'];     if ($url != "") echo "<img src=\"$image\" align=\"left\">";     echo "<a href=\"$url\" title=\"Learn More at Amazon.com\">$title<a><br>";     echo "Author(s): " . $authorList . "<br>";     echo "List Price: " . $price;     echo "<hr>"; } ?> 

This code was stolen from the displayBooks() function presented earlier.

Figure 7-7 shows random results from using the keyword PHP on the first run. Figure 7-8 shows random results from using the keyword PHP on the second run.

image from book
Figure 7-7

image from book
Figure 7-8

As you can see, this code effectively selects random items, using random keywords to keep the displays shown to your users fresh. To get an even wider variety of results, you may want to consider replacing the hard-coded value for page with a random number. Just be careful to ensure that the number is low enough to be valid for all search terms (or simply use a multidimensional array for the search terms, the first element being the term, the second being the number of pages available).

Note 

Presenting a list of Amazon products on your site may seem a little odd. However, it can be quite profitable. Many technical authors I know have commented that they make just as much money from Amazon selling their books from their website as they do in royalties from actually writing it!




Professional Web APIs with PHP. eBay, Google, PayPal, Amazon, FedEx, Plus Web Feeds
Professional Web APIs with PHP. eBay, Google, PayPal, Amazon, FedEx, Plus Web Feeds
ISBN: 764589547
EAN: N/A
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net