O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Amazon Hacks
By Paul Bausch
August 2003
More Info

HACK
#20
Add Multiple Items to a Wish List at Once
Speed up the process of adding items to your wish list with a bit of scripting
The Code
[Discuss (0) | Link to this hack]

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.

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 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 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.


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.