<? function GetBooks($SearchTerm) {
//
// written by Tim Lawrenz tim@lawrenz.com
//
// This Code Snipped is Public Domain
// You may use this code for free, modify it for your purposes, etc.
// but keep the name above.
//
// You can't make me responsible for any problems that may appear,
// especially NOT for problems with amazon.com and especially NOT
// if the code may stop working.
//
// The function GetBooks() receives a search term,
// looks up the best matches on the Amazon.com search-
// engine, checks, if a cover image is available
// and returns an array of maximum 9 items:
// returnvalue[0]["href"] = href to the book # 1's page on amazon.com
// returnvalue[0]["img"] = img src of book # 1's Cover
// returnvalue[0]["title"] = Title of book # 1
//
// returnvalue[1]["href"] = href to the book # 2's page on amazon.com
// returnvalue[1]["img"] = img src of book # 2's Cover
// returnvalue[1]["title"] = Title of book # 2
//
// etc... See code at bottom of the page how I use the function's
// results
//
// if the folder stored in $TempFolder exists, results are cached
// in that folder for up to $cache_time seconds.
//
// After having signed up at amazon.com's Associates Program, you
// got an AffilateID, please store it in the Var $YourAffilateID.
//
// YOUR PART ON
$YourAffilateID = "YourAffilatedID";
$cache_time = 5184000; // 5184000 means one day
$TempFolder = "../tmp/"; // YOUR PART OFF
//
//
$aBooksToShow = array();
$time = split( " ", microtime());
$cache_file = $TempFolder.urlencode($SearchTerm).amazoncache;
if (!file_exists($TempFolder)||(!(file_exists($cache_file))) || ((filectime($cache_file) + $cache_time - $time[1])<0) || (!(filesize($cache_file))) ) {
if (file_exists($TempFolder)) { $fpwrite = fopen($cache_file, "w"); }
$fp = fopen("http://www.amazon.com/exec/obidos/external-search/103-5685145-4397426?tag=$YourAffilateID&keyword=$SearchTerm", "r");
while(!feof($fp)) { $line .= fgets($fp,4096); }
$line = substr($line, strpos($line, '<li>'), strlen($line));
$line = substr($line, 0, strpos($line, '<font face=verdana,arial,helvetica size=-1>'));
if (strpos($line, "ASIN")>1) {
$aLines = explode('<li>', $line);
$i=0;
$Booksshown = 0;
while ($i<count($aLines) && $Booksshown <= 2) {
$URLParts = explode("/", $aLines[$i]);
$ASINNr = $URLParts[4];
$BookTitle = substr($aLines[$i], strpos($aLines[$i], ">")+1);
$BookTitle = substr($BookTitle, strpos($BookTitle, ">")+1);
$BookTitle = substr($BookTitle, strpos($BookTitle, ">")+1);
$BookTitle = substr($BookTitle, 0, strpos($BookTitle, "</a>"));
$Author = substr($aLines[$i], strpos($aLines[$i], "</a>")+4, 512);
$fp = @fopen('http://images.amazon.com/images/P/'.$ASINNr.'.01.MZZZZZZZ.gif', 'r');
if ($fp) {
$aBooksToShow[$Booksshown]["href"] = "http://www.amazon.com/exec/obidos/ASIN/$ASINNr/$YourAffilateID";
if (file_exists($TempFolder)) { fputs($fpwrite, $aBooksToShow[$Booksshown]["href"]."n"); }
$aBooksToShow[$Booksshown]["img"] = "http://images.amazon.com/images/P/$ASINNr.01.MZZZZZZZ.gif";
if (file_exists($TempFolder)) { fputs($fpwrite, $aBooksToShow[$Booksshown]["img"]."n"); }
$aBooksToShow[$Booksshown]["title"] = $BookTitle;
if (file_exists($TempFolder)) { fputs($fpwrite, $aBooksToShow[$Booksshown]["title"]."n"); }
$Booksshown++;
}
$i++;
}
if (file_exists($TempFolder)) { fclose($fpwrite); }
}
} elseif (file_exists($cache_file)) {
$fpread = fopen($cache_file, "r");
while (!feof($fpread)) { $BooksToShow = $BooksToShow.fread($fpread, 8192); }
fclose($fpread);
$aTempBooksToShow = explode("n", $BooksToShow);
$i=0;
while ($i<=2 && $i<count($aTempBooksToShow)) {
$aBooksToShow[$i]["href"] = $aTempBooksToShow[$i*3];
$aBooksToShow[$i]["img"] = $aTempBooksToShow[1+$i*3];
$aBooksToShow[$i]["title"] = $aTempBooksToShow[2+$i*3];
$i++;
}
}
return $aBooksToShow;
}
// FUNCTION ENDS HERE
// DEMO CODE STARTS HERE
$Books = GetBooks('test');
echo "<table border="0"><tr>";
$i=0;
while ($i<=2) {
if ($Books[$i]["href"]!='') {
echo '<td width="33%" align="center" valign="top">';
echo '<a href="'.$Books[$i]["href"].'">';
echo '<img src="'.$Books[$i]["img"].'"><br>'.$Books[$i]["title"].'</a><br>';
echo "</td>n";
}
$i++;
}
echo "</tr></table>"; // DEMO CODE ENDS HERE ?>
|
|