Hack 20 Add Multiple Items to a Wish List at Once

figs/expert.giffigs/hack20.gif

Speed up the process of adding items to your wish list with a bit of scripting.

Creating a wish list with 15-20 products is usually a slow process, adding one or two items while you're browsing. Amazon doesn't offer an interface for adding multiple items to a wish list, but you can build one with a bit of scripting. This PHP script accepts a comma-separated list of ASINs and adds them to your wish list all at once.

20.1 What You Need

This script relies on automatic form posts, and there are some external PHP tools to speed up this kind of development. Snoopy for PHP is an open source class that handles HTTP requests. You can download a copy from SourceForge (http://sourceforge.net/projects/snoopy/) and include it in the same directory as this script.

20.2 The Code

This script runs as a web page and presents a simple form for entering ASINs. Once you enter a list of ASINs, this script performs three main actions:

  1. With the Snoopy PHP class, it simulates a Sign In form post at Amazon with your email address and password to retrieve a session ID.

  2. It then loops through the ASINs.

  3. For each ASIN, it uses Snoopy to send a form post with the variables from the remote wish list form [Hack #19].

Because you need to be signed in to add items to your wish list, the key to this script is obtaining a 17-digit Amazon session ID [Hack #13] that is associated with your account when you visit the site. Be sure to include your email address and Amazon password in the following code. Your Amazon password will be in the source of the file, so make sure you're the only one with access. You can also insert an associate tag Section 5.2.2 if you have one, to make sure you get a commission on your wish list purchases.

<?php
# add_wishlist_items.php
# Adds multiple items to an Amazon Wish List
# Usage: A PHP page called via web browser

$aff_tag = "insert associate tag ";

error_reporting(E_ERROR | E_WARNING); 

if ($HTTP_POST_VARS) {
   include "Snoopy.class.inc";
   $snoopy = new Snoopy;

   $snoopy->agent = "(compatible; MSIE 4.01; MSN 2.5; AOL 4.0; Windows 98)";
   $submit_url = "https ://www.amazon.com/exec/obidos/flex-sign-in-done/";
    
   $submit_vars["email"] = "insert email address ";
   $submit_vars["password"] = "insert Amazon password ";
    
   $submit_vars["method"] = "get";
   $submit_vars["opt"] = "oa";
   $submit_vars["page"] = "recs/instant-recs-sign-in-standard.html";
   $submit_vars["response"] = "tg/recs/recs-post-login-dispatch/-".
                              "/your/pd_ys_fr_ur";
   $submit_vars["action"] = "sign-in";
   $submit_vars["next-page"] = "recs/instant-recs-register-standard.html";

    //submit email and password to start a session
    if($snoopy->submit($submit_url,$submit_vars))
    {
        while(list($key,$val) = each($snoopy->headers)) {
            if (preg_match("/session-id=(.*?);/i", $val, $ubid)) {
                $session_id = $ubid[1];
            }
        }
    } else {
        echo "error fetching document: ".$snoopy->error."\n";
    }
    
   $snoopyAdd = new Snoopy;
   $snoopyAdd->agent = "(compatible; MSIE 4.01; MSN 2.5; AOL 4.0; ".
                       "Windows 98)";
    
   $ASIN_list = split(",",$_POST['txaASINs']);
   $submit_add["tag-value"] = $aff_tag;
   $submit_add["tag_value"] = $aff_tag;
   $submit_add["submit.add-to-registry.wishlist"] = [RETURN]
             "Add to Amazon.com Wish List";
   $cntAdded = 0;
   for($i = 0;$i < count($ASIN_list); $i++) {
        $thisASIN = trim($ASIN_list[$i]);
        $submit_url = "http://www.amazon.com/o/dt/assoc/handle-buy".
                      "-box=".$thisASIN."/".$session_id;
        $submit_add["asin.".$thisASIN] = "1";
        if(!$snoopyAdd->submit($submit_url,$submit_add)) {
            echo "error adding item ".$thisASIN.": ".
                 $snoopyAdd->error."<br>\n";
        } else {
            $cntAdded++;
        }
        unset($submit_add[$thisASIN]);
   }
   echo $cntAdded . " item(s) added to your wishlist!";
} else {
?>
<html>
<head>
    <title>Add Wishlist Items</title>
</head>

<body>
Add a list of ASINs below, separated by commas:
<br>
<form action="add_wishlist_items.php" method="post">
    <textarea cols="35" rows="6" name="txaASINs"></textarea>
    <br><input type="submit" value="Add Items">
</form>
</body>
</html>
<?php } ?>

Note the https:// in the section that signs in to Amazon. As the script gets a session ID, the POST is made over an SSL connection?so you can be sure that using this script is as secure as signing in at Amazon personally.

20.3 Running the Hack

Add the code to a file called add_wishlist_items.php and upload it to your web server. Bring up the page in your web browser, enter a list of ASINs separated by commas (e.g., 0596004478, 0596004605, 0596004613) and click "Add Items." A page should appear letting you know that three (or however many you entered) items were added to your wish list. Just visit your wish list on Amazon to verify!