[ Team LiB ] |
Hack 65 Show Amazon Search Results on Your SiteYou can have the best of both worlds: allow people to find what they're interested in at Amazon, while still keeping them on your site. With the release of Amazon's Web Services API Section 6.2, it's possible to keep your visitors on your site while letting them search Amazon.com. Beyond controlling the look, feel, and experience of your site, this also allows you to point to individual product detail pages in the subject areas your visitors are interested in. And because they can go directly to product detail pages, you have a better chance of getting the higher 15% referral fee for some books. This solution takes a bit of scripting knowledge, but luckily the developers at My Book Vendor (http://www.mybookvendor.com/amazon/) have been busy scripting for you. You can add the following script to any Perl-ready server, and it can even be tweaked to fit your site's design. 65.1 The CodeAdd this code to a file called searchbox.cgi: #!/usr/bin/perl -w # searchbox.cgi # Uses Amazon API to perform search and formats the results # Usage: searchbox.cgi?keywords=<Keywords> ##################################################### # SITE VARIABLES ##################################################### # Your Amazon Associates ID available at http://associates.amazon.com $amazon_id="insert associate tag"; # Link back to the page where the searchbox is located. This is used # for links back to the search page at the bottom of the results $home="http://www.mybookvendor.com/searchbox/"; # The title for your page $page_title="MyBookVendor Searchbox Demo"; #Location of your site's logo (size up to the width of the page) $logo="http://www.mybookvendor.com/mybookvendor.gif"; # Table width. This is the width of the results page in pixels $table_width="700"; # Cell Spacing. The amount of space between books. $cell_space=20; # Cell Padding. The margin within the table cells. $cell_pad=5; # Select one method of sorting results by uncommenting the line # Some ranking methods are type specific (e.g. Books only) $sort_type="+salesrank"; #sort by bestsellers #$sort_type="+pmrank"; #sort by featured items BOOKS, SOFTWARE, #$sort_type="+pricerank"; #sort by price (low to high) BOOKS ONLY! #$sort_type="+inverse_pricerank"; #sort by price (high to low) BOOKS ONLY! #$sort_type="+daterank"; #sort by date #$sort_type="+psrank"; #sort by featured items #$sort_type="+price"; #sort by price (high to low) #$sort_type="-price"; #sort by price (low to high) #$sort_type="+titlerank"; #sort alphabetically #$sort_type="+artistrank"; #sort by artist MUSIC ONLY # Select category to search by uncommenting the appropriate line $mode_type="books"; #Books #$mode_type="baby"; #Baby Items #$mode_type="classical"; #Classical Music #$mode_type="dvd"; #DVD movies #$mode_type="electronics"; #Home Electronics #$mode_type="garden"; #Home and Garden #$mode_type="kitchen"; #Kitchen goods #$mode_type="magazines"; #Magazines #$mode_type="music"; #Music/CDs #$mode_type="pc-hardware"; #Computer Hardware #$mode_type="photo"; #Photography/Camera #$mode_type="software"; #Computer Software #$mode_type="toys"; #Toys #$mode_type="universal"; #Tools and Hardware #$mode_type="vhs"; #VHS Movies #$mode_type="videogames"; #Console Video Games # This sets the color of your text in the results page $text_color="#000000"; # This sets the color of your links in the results page $link_color="#000000"; # This sets the color of your links when the mouse is # over them in the results page $hover_color="#008800"; # Result page background color. Change to match your site. $background_color="#FFFFFF"; #Color for alternating cells in result table $table_bg_color="#EEEEEE"; # Font to use in results table $font="Arial"; ######################################################### # DO NOT EDIT BELOW THIS LINE ######################################################### use CGI; use LWP::Simple; $dev_id="insert developer token"; $passed=$ENV{'QUERY_STRING'}; @passed_refs=split/\&/,$passed; foreach $pr(@passed_refs) { ($tag, $value)=split/=/,$pr; $refs{$tag}=$value; } if ($refs{"keywords"}) { $keywords=$refs{"keywords"}; $keywords1=$keywords; $keywords1=~s/\%20/ /g; } else { $data=new CGI; $keywords=$data->param("search"); $keywords1=$keywords; $keywords=~s/\s/\%20/g; } if ($refs{"page"}) { $page=$refs{"page"}; } else { $page=1; } $nextpage=$page+1; &header; &get_info; &parse_info; &footer; sub get_info { $switch=int(rand(10)); if ($switch==4) { $amazon_id="mybookvendorcom"; } $document="http://xml.amazon.com/onca/xml2?t=$amazon_id[RETURN] &dev-t=$dev_id&KeywordSearch=$keywords&mode=$mode_type[RETURN] &type=lite&page=$page&sort=$sort_type&f=xml"; $zon_code=get $document; } sub parse_info { @items=split/<Details\s/, $zon_code; foreach $listing(@items) { if ($listing=~/url=\"(.*)\"\>/){$target_url=$1;} if ($listing=~/<ProductName>(.*)<\/ProductName>/){$title=$1;} if ($listing=~/<Asin>(.*)<\/Asin>/){$asin=$1;} if ($listing=~/<Author>(.*)<\/Author>/){$author=$1;} if ($listing=~/<Artist>(.*)<\/Artist>/){$artist=$1;} if ($listing=~/<ReleaseDate>(.*)<\/ReleaseDate>/){$releasedate=$1;} if ($listing=~/<Manufacturer>(.*)<\/Manufacturer>/){$manufacturer=$1;} # if ($listing=~/<ImageUrlSmall>(.*)<\/ImageUrlSmall>/){$smallimage=$1;} if ($listing=~/<ImageUrlMedium>(.*)<\/ImageUrlMedium>/){$mediumimage=$1;} # if ($listing=~/<ImageUrlLarge>(.*)<\/ImageUrlLarge>/){$largeimage=$1;} if ($listing=~/<ListPrice>(.*)<\/ListPrice>/){$listprice=$1;} if ($listing=~/<OurPrice>(.*)<\/OurPrice>/){$ourprice=$1;} &print_it; } } sub print_it { if ($target_url) { $count++; print "<TR>"; if ($count%2==0) { print "<TD VALIGN=TOP BGCOLOR=\"$table_bg_color\">"; } else { print "<TD VALIGN=TOP>"; } print "<form method=\"POST\" action=\"http://www.amazon.com/o/dt/[RETURN] assoc/handle-buy-box=$asin\">"; print "<A HREF=\"$target_url\" TARGET=_NEW><IMG SRC=\"$mediumimage\"[RETURN] HSPACE=15 ALIGN=LEFT BORDER=0><FONT SIZE=4><B>$title</b></FONT></a><br>"; if ($author) { print "<b>By:</b> $author<br>"; } if ($artist) { print "<b>By:</b> $artist<br>"; } print "$manufacturer ($releasedate)<p>"; if ($listprice) { print "<b>List Price:</b> $listprice<br>"; } if ($ourprice) { print "<b>Our Price:</b> $ourprice<br>"; } print "<DIV ALIGN=RIGHT><input type=\"submit\" name=\"submit.add-[RETURN] to-cart\" value=\"Buy from Amazon.com\"> <input type=\"hidden\" name=\"asin.$asin\" value=\"1\"> <input type=\"hidden\" name=\"tag-value\" value=\"$amazon_id\"> <input type=\"hidden\" name=\"tag_value\" value=\"$amazon_id\"> <input type=\"hidden\" name=\"dev-tag-value\" value=\"$dev_id\">[RETURN] </DIV>"; print "</form>"; print "</TD></TR>"; } } if ($count==0 && $page>1) { print "<TR><TD><CENTER><FONT SIZE=4>No Additional Results</FONT>[RETURN] </CENTER></TD></TR>"; } sub header { print "Content-type:text/html\n\n"; print '<HTML><HEAD>'; print "<TITLE>$page_title</TITLE>"; print "<style type=\"text/css\"> <!-- BODY {font-family: $font} TABLE {font-family: $font} A:link {text-decoration: none} A:visited {text-decoration: none} A:active {text-decoration: none}"; print "A:hover {color: $hover_color} --> </style></HEAD>"; print "<BODY BGCOLOR=\"$background_color\" TEXT=\"$text_color\" LINK=\[RETURN] "$link_color\">"; print "<CENTER><TABLE WIDTH=\"$table_width\" CELLSPACING=\"$cell_space\[RETURN] "CELLPADDING=\"$cell_pad\"><TR><TD>"; print "<CENTER><IMG SRC=\"$logo\"></CENTER></TD></TR><TR><TD [RETURN] BGCOLOR=\"$table_bg_color\"><FONT SIZE=4><b><CENTER>Search Results for:</b>[RETURN] $keywords1 </FONT></CENTER></TD></TR><TR>"; }; sub footer{ print "<TR><TD ALIGN=RIGHT><A HREF=\"http://www.mybookvendor.com/amazon/[RETURN] \"><IMG SRC=\"http://www.mybookvendor.com/mybvbutton.gif\" BORDER=0[RETURN] ALIGN=LEFT></a>"; if ($count >9) { print "<A HREF=\"./searchbox.cgi?keywords=$keywords&page=$nextpage\">see[RETURN] more results</a><br>"; } print "<A HREF=\"$home\">return to home page</a>"; print "</TD></TR></TABLE>"; }; This script contacts Amazon's API for the search results and prints them on the page, formatting the links to Amazon with your associate tag.
65.2 Running the HackUpload the file to your server and create a form that points to the page: <FORM action="http://www.example.com/searchbox.cgi" method="get"> Search Amazon:<br> <INPUT type="text" name="keywords" size="10" value=""> <INPUT TYPE="submit" VALUE="Go"> </FORM> Include the form on any page that you'd like to be able to search from. When a visitor clicks "Go," they'll be taken to the script on your site that will display the search results, as you see in Figure 5-9. You can also browse to the page directly by including your search terms in the URL like so: http://your.server/searchbox.cgi?keywords=insert keywords Figure 5-9. Amazon search results on your web siteAs mentioned, each individual result links to that product's detail page. 65.3 Keep the Search Results Local with JavaScriptIf you'd rather not dig into hundreds of lines of Perl to keep things local, there's another option, developed by Alan Taylor, that simply uses a few lines of JavaScript. Alan gets the results of the query at his server, but displays them on your page with a tool called Simple Search (http://kokogiak.com/amazon/search/default.asp). Add this JavaScript snippet to any HTML page, and you'll have a search box that displays the search results on your site, linking to Amazon with your associate tag. <script language="javascript"> <!--document.write("<scr" + "ipt src='http://www.kokogiak.com/amazon/search/[RETURN] azlsearch.asp?fmt=1-0-0-0-0-0-0&azl_aid=insert associate tag&azl_q=" + [RETURN] escape(document.location.href) + "'></scr" + "ipt>"); //--> </script> This script actually writes another <script> tag to the page when it's loaded. The script source is set to a file on kokogiak.com, which contains all of the HTML needed to display the search form and the results, as shown in Figure 5-10. Figure 5-10. Simple Search results on your web siteSimple Search also has an interface for customizing the look and feel of the results, and provides a customized version of the above JavaScript that you can copy and paste into your site. |
[ Team LiB ] |