Zend - The PHP Company




Commerce

Add Code


Amazon Affiliate "Optimizer"  

Type: code fragment
Added by: mcobby
Entered: 31/08/2000
Last modified: 31/10/2008
Rating: **** (5 votes)
Views: 9621
provides a normal search interface to Amazon, which works at 5% fees, and takes the results and turns them into "individually linked books"which should earn 15% but does so while preserving the look and feel of amazon.


<?
/*
 * Title:      Amazon Affiliate "Optimzier"
 * Author:     Matthew Cobby, matt@andamooka.com, http://www.andamooka.com
 * Date:       24th August 2000
 * Version:    1.0
 *
 * Desc:       Vaguely inspired by the Amazon Affiliate Grabber by
 *             Tim Lawrenz, tim@lawrenz.com (availabe from http://www.zend.com).
 *             This script goes one step further by providing a normal
 *             search interface to Amazon, which works at 5% fees, and takes
 *             the results and turns them into "individually linked books"
 *             which should earn 15% but does so while preserving
 *             the look and feel of amazon.  It also works for the US, UK
 *             and German versions of the site.
 * Module:     Part of the Andamooka Toolkit
 *
 * Disclaimer: If it works, it works.  If it doesn't then good luck.  This
 *             software is provided "as is" with absolutely no guarantee.
 *             Check your operating agreements with Amazon before using this
 *             because I'm not sure where this technique lies.
 *
 *
 * Copyright 2000 Andamooka Technologies Ltd
 *
 */

// Leave this alone, holds everything required to describe an Amazon interface
$affiliates = array();

//-------------------------------------------------------------------------
//                     USER MODIFIABLE VARIABLES
//-------------------------------------------------------------------------

// Enter your details here so it can generate your forms.
// If you dont want an image or don't have a URL set the string to null
// and they will disappear.
$yourName 'TravelZilla';
$yourImage 'http://www.travelzilla.com/images/mainlogo.gif';
$yourLink  'http://www.travelzilla.com';

// Enter your AMAZON.COM affiliate ID here
$affiliates["amazon.com"]["affiliateID"] = 'affiliateID';

// Enter your AMAZON.CO.UK affiliate ID here
$affiliates["amazon.co.uk"]["affiliateID"] = 'affiliateID';

// Enter your AMAZON.DE affiliate ID here
$affiliates["amazon.de"]["affiliateID"] = 'affiliateID';

/*
 * if you won't have (or want) an affiliate ID with all of the above,
 * then just set the string to null and the respective form won't be
 * generated.
 */

//--------------------------------------------------------------------------
//                     END OF USER MODIFIABLE VARIABLES
//
//  If you want a simple life, don't mess with anything below here ;-)
//-------------------------------------------------------------------------


/*
 *  This next section describes each of the amazon sites.  It contains
 *  information such as name, url, images url and the values for
 *  the product selector in the search form (i.e. the DropDown in the
 *  search form).
 *
 *  It's not the most elegant way of doing it but at least it keep its
 *  all together for now.  I've tried to write the code so that it will
 *  be laregly unaffected if any of the sites change their HTML, always a big
 *  problem with site grabbers.  Seperating out the site specific details
 *  like this should help but it's not a promise!
 */

// Amazon.com
// Each Amazon has different ranges of products and different dropdown
// menus.  This array is an attempt @ describing the products and their types.
$amazonComDD = array();
$amazonComDD["All Products"]            = "blended";
$amazonComDD["Books"]                   = "books";
$amazonComDD["Classical Music"]         = "classical-music";
$amazonComDD["Video"]                   = "vhs";
$amazonComDD["DVD"]                     = "dvd";
$amazonComDD["Toys & Games"]            = "toys";
$amazonComDD["Electronics"]             = "electronics";
$amazonComDD["Software"]                = "software";
$amazonComDD["Tools & Hardware"]        = "universal";
$amazonComDD["Lawn & Patio"]            = "garden";
$amazonComDD["Kitchen"]                 = "kitchen";
$affiliates["amazon.com"]["name"]       = "Amazon.com";
$affiliates["amazon.com"]["url"]        = "http://www.amazon.com";
$affiliates["amazon.com"]["imageurl"]   = "http://images.amazon.com";
$affiliates["amazon.com"]["formimage"]  = "http://www.amazon.com/g/associates/logos2000/126X32-b-logo.gif";
$affiliates["amazon.com"]["selectname"] = "index";
$affiliates["amazon.com"]["select"]     = $amazonComDD;

// Amazon.co.uk
$amazonCoUkDD = array();
$amazonCoUkDD["All Products"]             = "blended";
$amazonCoUkDD["Books"]                    = "books";
$amazonCoUkDD["DVD & Video"]              = "video";
$amazonCoUkDD["Popular Music"]            = "music";
$amazonCoUkDD["Classical Music"]          = "classical";
$amazonCoUkDD["Software"]                 = "software-uk";
$amazonCoUkDD["PC & Video Games"]         = "video-games-uk";
$affiliates["amazon.co.uk"]["name"]       = "Amazon.co.uk";
$affiliates["amazon.co.uk"]["url"]        = "http://www.amazon.co.uk";
$affiliates["amazon.co.uk"]["imageurl"]   = "http://images.amazon.com";
$affiliates["amazon.co.uk"]["formimage"]  = "http://www.associmg.com/assoc/uk/uk_searchbox.jpg";
$affiliates["amazon.co.uk"]["selectname"] = "mode";
$affiliates["amazon.co.uk"]["select"]     = $amazonCoUkDD;

// Amazon.de
// My german isn't that good but I think I've got this one right.
$amazonDeDD = array();
$amazonDeDD["Alle Produkte"]            = "blended";
$amazonDeDD["B�cher"]                   = "books";
$amazonDeDD["US-B�cher"]                = "us";
$amazonDeDD["Pop Musik"]                = "music";
$amazonDeDD["Song Titel"]               = "music-tracks";
$amazonDeDD["Klassik"]                  = "classical-music";
$amazonDeDD["DVD & Video"]              = "video";
$amazonDeDD["PC- & Videospiele"]        = "video-games-de";
$amazonDeDD["Software"]                 = "software-de";
$affiliates["amazon.de"]["name"]        = "Amazon.de";
$affiliates["amazon.de"]["url"]         = "http://www.amazon.de";
$affiliates["amazon.de"]["imageurl"]    = "http://images.amazon.com";
$affiliates["amazon.de"]["formimage"]   = "http://www.amazon.de/g/associates/assocnew/amazon-black.gif";
$affiliates["amazon.de"]["selectname"]  = "mode";
$affiliates["amazon.de"]["select"]      = $amazonDeDD;

//--------------------------------------------------------------------------
//                              FUNCTIONS
//--------------------------------------------------------------------------

/*
 * This function is where it all happens.  Takes a series of parameters
 * to describe which affiliate you want to search and then opens up a
 * connection, retrieves the results (in HTML) and kind of monkeys
 * around with the hrefs before returning the "all new and improved"
 * results.
 */

function GetAffiliatePage ($affiliateURL$affiliateID$searchKeywords$selectName$product)
{
   
$searchKeywords httpEscape ($searchKeywords);
   
$results getSearchResults ($affiliateURL,$affiliateID,$searchKeywords,$selectName,$product);
   
$page changeLinks ($results$affiliateURL$affiliateID);

   return 
$page;
}


//--------------------------------------------------------------------------

/*
 * Translates the keywords into a format suitable for a URL.
 */
function httpEscape ($data)
{
   return (
rawurlencode($data));
}

//--------------------------------------------------------------------------

/*
 * Provides the interface with the Amazon server of choice.  Iniatates
 * search and dumps raw results back.
 *
 * To Do:  Pulling back a new search every time someone uses this page
 *         could be a bit wasteful on bandwidth since you also then
 *         have to send the data back to the client.  i.e. 2 x page
 *         for every search.  Could really use some for of caching here.
 *         I know Tim Lawrenz's Amazon script has some form of caching
 *         but I've not really had time to think about it in any detail.
 *         Any suggestions?
 */
function getSearchResults($affiliateURL,$affiliateID,$search$typeName$type)
{
   
// Could perhaps use some form of caching here?

   // opens up a URL to your amazon site for search results
   
$url "$affiliateURL/exec/obidos/external-search/103-5685145-4397426?tag=$affilateID&keyword=$search&$typeName=$type";
   
$fp fopen($url"r");

   
// read the data into one variable
   
while(!feof($fp)) { $line .= fgets($fp,4096); }

   return (
$line);
}

//--------------------------------------------------------------------------

/*
 *  This is the important bit.  It turns all nominal 5% revenue
 *  links into 15% by mucking around with the HTML and making
 *  each link look like an "individual recommendation"
 */
function changeLinks($data$affiliateURL$affiliateID)
{
   
// convert all product references into affilate earning money!

    
$patterns = array (
            
'#href=("?)/exec/obidos/asin/(w*?)/(.*?)>#i',   // Converts all into 15% fee-earning links
                
'#src=("?)/(.*?)>#i',      // Turns relative URLS into absolute
            
'#href=("?)/(.*?)>#i',     //   "      "      "    "    "
            
'#action=("?)/(.*?)>#i',   //   "      "      "    "    "
         
);

    
$replace  = array (
                
"href=\1$affiliateURL/exec/obidos/ASIN/\2/$affiliateID\1>",
                
"src=\1$affiliateURL/\2>",
                
"href=\1$affiliateURL/\2>",
        
"action=\1$affiliateURL/\2>"
        
);

    
$data preg_replace ($patterns$replace$data);
    return (
$data);

// changeLinks function


//-------------------------------------------------------------------------

/*
 * Does what it says! Prints out an Amazon search form for every affiliate
 * ID that you specified (remember that bit at the top?). Don't want all 3
 * forms? Then dont specify an ID.
 */
function printForm($affiliates$yourName$yourImage$yourLink)
{
   
$dd '';
   
$num 0;

   
?> <p align="center">  <?

   
if ($yourImage != '')
   {
    if(
$yourLink!=''){echo "<a href="$yourLink">";}
    echo 
"<img src="$yourImage" alt="$yourName" border="0"><br>";
    if(
$yourLink!=''){echo '</a>';}
   }

   
?>
        <font face="verdana,arial,helvetica">
   <? if($yourLink!=''){echo "<a href="$yourLink">$yourName</a>";}else{echo $yourName;} ?> in association with ...</p>

   <?
   
foreach($affiliates as $affiliate => $curr)
   {
      
$num++;
      if (
$curr["affiliateID"] != '')
      {
         
// i.e. if you have entered an affiliate ID print the form out
         
?>

         <center>
<FORM METHOD="post" ACTION="<? echo $REQUEST_URI ?>">

<input type="hidden" name="yourName" value="<? echo $yourName?>">
<input type="hidden" name="affiliateID" value="<? echo $curr["affiliateID"]; ?>">
<input type="hidden" name="affiliateName" value="<? echo $curr["name"]; ?>">
<input type="hidden" name="affiliateURL" value="<? echo $curr["url"]; ?>">
<input type="hidden" name="affiliateImageURL" value="<? echo $curr["imageurl"]; ?>">
<input type="hidden" name="affiliateSelectName" value="<? echo $curr["selectname"]; ?>">

<TABLE BORDER="0" CELLPADDING="1" cellspacing="0" BGCOLOR="#000000"><TR><TD>
<TABLE BORDER="0" CELLPADDING="2" cellspacing="0" align="center" BGCOLOR="#FFFFFF">
<TR BORDER="0">
<TD BGCOLOR="#FFFFFF" align="RIGHT" valign="middle"><font face="verdana,arial,helvetica" size="-2"><B>Search:</B></font></TD>
<TD BGCOLOR="#FFFFFF" align="left" valign="middle"><font face="verdana,arial,helvetica" size="-2">
<SELECT NAME="product">

<?
   $dd 
$curr["select"];
   foreach (
$dd as $key => $value)
   {
      echo 
"<option value="$value"";
      if (
$value=="books"){ echo " selected "; }
      echo 
">$key</option>";
   }  
// end foreach
?>

</SELECT>
</font></TD>
<TR BORDER="0">
<TD BGCOLOR="#FFFFFF" align="RIGHT" valign="middle"><font face="verdana,arial,helvetica" size="-2"><B>Keywords:</B></font></TD>
<TD BGCOLOR="#FFFFFF" align="left" valign="middle"><font face="verdana,arial,helvetica" size="-2">
<INPUT TYPE="text" NAME="keyword" SIZE="10" VALUE="">
<!-- <INPUT TYPE=hidden NAME=tag VALUE=your_associates_id> -->
<INPUT TYPE="submit" BORDER=0 VALUE="Go!" NAME="Search">
</font></TD>
</TR>
<TR>
<TD COLSPAN="2" VALIGN="top" ALIGN="center" BGCOLOR="#000000">
<IMG SRC="<? echo $curr["formimage"]; ?>" BORDER="0" HSPACE="0" VSPACE="0" ALT="In Association with <? echo $curr["name"]; ?>"></a>
</TD>
</TR>
</TABLE></TD></TR></TABLE>
</FORM>
</center>
      <?
      
// end if

   
// end foreach
   
if ($num>0)
   {
      
?> <center><font face="verdana,arial,helvetica">Please choose the Amazon closest to you.</font></center> <?
   
}
// end of printForm();


//--------------------------------------------------------------------------

/*
 * This is the main function.  If page is being viewed for a first time
 * then it prints out the forms, if there is a search in progress then
 * it will dump out the results
 */

// API is now defined, let's get on with it.
if (isset($affiliateID))
{
   
?>
<font face="arial" size="+1"><b><? echo $yourName ?></b> In Association With <b><? echo $affiliateName ?></b></font><P>
   <?

   
// if we have a search to perform
   
echo  GetAffiliatePage ($affiliateURL$affiliateID$keyword$affiliateSelectName$product);

}
else
{
   
// print the default page.
   
printForm($affiliates$yourName$yourImage$yourLink);
}

//--------------------------------------------------------------------------

// Have fun.


Usage Example




Rate This Script





Search



This Category All Categories