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
#36
Share the Love (and Savings!) with Your Friends
Amazon's Share the Love program lets you and your friends save money by buying the same products. Here's a way to keep track of who wants what
The Code
[Discuss (0) | Link to this hack]

The Code

This PHP script performs two separate actions depending on what variables are passed to it. If someone is adding a product they'd like to receive a discount on, it adds their email address to a text file. (The file is written to the same directory as the script with the name of the ASIN as the title, ASIN.db.) If someone is looking for email addresses to send "the love" (discount) to, it displays all of the email addresses saved in the ASIN text file. Create a PHP file called share_love.php and add the following code:

<?php
// share_love.php
// A Web page that associates ASINs with email addresses
// and then displays them. This helps take advantage of
// Amazon's *Share the Love* discount system.
if ($HTTP_GET_VARS) {
    if ($HTTP_GET_VARS['sub']=="add item") {
        $file=($HTTP_GET_VARS['asin'] . ".db");
        $fp=fopen("$file", "a+");
        fwrite($fp, $HTTP_GET_VARS['email']);
        fclose($fp);
        echo "Asin added!<br><br>";
    } elseif ($HTTP_GET_VARS['sub']=="get addresses") {
        $file=($HTTP_GET_VARS['asin'] . ".db");
        if (file_exists($file)) {
          echo "<h2>Sharing Love</h2>";
          echo "Paste these addresses into Amazon's Share the Love";
          echo " form:<br><br>";
            $fp=fopen("$file", "r");
            $contents = fread ($fp, filesize ($file));
            $a_adds = split("\n", $contents);
            if (count($a_adds) > 1) {
                for($i = 0;$i < count($a_adds)-1; $i++) {
                    echo $a_adds[$i] . ", ";
                }
            } else {
                echo $contents;
            }
        } else {
            echo "<h2>No Love</h2>";
            echo "No email addresses for this ASIN.";
        }
    }
} else {
?>
<html>
<body>
<h2>Add Product</h2>
<form action="share_love.php" method="get">
ASIN:<br><input type="text" name="asin" size="10"><br><br>
Email:<br><input type="text" name="email" size="25"><br><br>
<input type="submit" name="sub" value="add item">
</form>
<br><br>
<h2>Get Addresses</h2>
<form action="share_love.php" method="get">
ASIN:<br><input type="text" name="asin" name="" size="10"><br><br>
<input type="submit" name="sub" value="get addresses">
</form>
</body>
</html>
<?php } ?>


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.