<?php

//this is a simple stand-alone PHP example script
//for the lite version of the new Amazon Web Services interface
//20020816..29/Jaap van Ganswijk <ganswijk@xs4all.nl>, released under the GNU GPL

//you normally would like to change the following two variables
//(but the script should also work as it is)
$search         ='machiavelli';  //replace this with your own search term, you can give upto 3 terms separated by '%20' like 'cat%20dog', a '+' should also work instad of %20
//$search       ='title:"civil+engineering"';  //replace this with your own search term, you can give upto 3 terms separated by '%20' like 'cat%20dog', a '+' should also work instad of %20
$associates_id='chipdir';  //replace this with your own associate id
//get one from http://associates.amazon.com/exec/panama/associates/apply

//--You don't have to change anything after this line, to start earning money.--

$dev_token='D2WMCOIPS9D14E';  //replace this with your own developer token if you like
//get one from http://associates.amazon.com/exec/panama/associates/join/developer/application.html
//you don't have to change this token unless you change the code a lot it seems:
//http://forums.prospero.com/n/mb/message.asp?webtag=am-assocdevxml&msg=378.1&ctx=0

$mode='books';  //change this if you want to offer something else as books
//other modes are:
//mode=baby        (Baby) 
//mode=books       (Books) 
//mode=classical   (Classical Music) 
//mode=dvd         (DVD) 
//mode=electronics (Electronics) 
//mode=garden      (Outdoor Living) 
//mode=kitchen     (Kitchen & Housewares) 
//mode=magazines   (Magazines) 
//mode=music       (Popular Music) 
//mode=pc-hardware (Computers) 
//mode=photo       (Camera & Photo) 
//mode=software    (Software) 
//mode=tools       (Tools & Hardware) 
//mode=toys        (Toys & Games) 
//mode=vhs         (Video) 
//mode=videogames  (Computer & Video Games) 

# search parameters:
$type    ='lite';  //don't use this script for the 'heavy' XML!
$show_xml=false;   //change false to true if you want to see the raw XML data (for debugging)

$f_rich  =true;    //this will convert the URL so you'll get 15% on qualifying books instead of 5%.
                   //change it to false when you prefer to get only 5% ;-)
                   //see http://forums.prospero.com/n/mb/message.asp?webtag=am-assosdev&msg=6.1&ctx=0
                   //I'm not sure if this method will also work for non-books, but I guess so

if (!$page) {
  
$page=1;
}

$file="onca/xml3?".
      
"dev-t=$dev_token".'&'.
      
"f=xml".'&'.
      
"KeywordSearch=$search".'&'.
      
"mode=$mode".'&'.
      
"page=$page".'&'.
//    "PowerSearch=$search".'&'.
      
"t=$associates_id".'&'.
      
"type=$type".'&'.
      
"v=1";
//Please note that:
//- /onca/xml3 is the pathname of the URL
//- ? denotes the spot where the arguments start
//- that all arguments consist of 'argument_name=value' and that I have put them in alphabetical order
//- that '&' seperates the arguments
//you can also add:
//- page=1 for result entries  1..10
//- page=2 for result entries 11..20
//- etc.
//Each query result will contain only up to 10 entries. If you want to show more than 10 results, you will have to show several pages with results until you get a result with 0..9 entries, but when it's 0 you'll get an error message instead.
//To search for ASIN's, replace the KeywordSearch line by AsinSearch=<ASIN>,<another ASIN> upto 30 for lite and 10 for heavy searches.
//To search for UPC's use instead: UpcSearch=<upc>,<upc>.
//You can also use:
//- AuthorSearch      =.. (only for mode=books)
//- ArtistSearch      =.. (only for mode=music,classical)
//- ActorSearch       =.. (only for mode=dvd,vhs,video)
//- DirectorSearch    =.. (only for mode=dvd,vhs,video)
//- ManufacturerSearch=.. (only for mode=electronics,kitchen,videogames,software,photo,pc-hardware)
//- SimilaratiesSearch=ASIN (no mode)

//Amazon lists (in the lite version) per book a record like:
// <Details url="http://www.amazon.com/exec/obidos/redirect?tag=chipdir%26creative=D2WMCOIPS9D14E%26camp=2025%26link_code=xm2%26path=ASIN/039480001X">
//    <Asin>039480001X</Asin>
//    <ProductName>The Cat in the Hat</ProductName>
//    <Catalog>Book</Catalog>
//    <Authors>
//       <Author>Seuss</Author>
//       <Author>Theodor Seuss Geisel</Author>
//       <Author>Dr. Seuss</Author>
//    </Authors>
//    <ReleaseDate>June, 1957</ReleaseDate>
//    <Manufacturer>Random House (Merchandising)</Manufacturer>
//    <ImageUrlSmall>http://images.amazon.com/images/P/039480001X.01.THUMBZZZ.jpg</ImageUrlSmall>
//    <ImageUrlMedium>http://images.amazon.com/images/P/039480001X.01.MZZZZZZZ.jpg</ImageUrlMedium>
//    <ImageUrlLarge>http://images.amazon.com/images/P/039480001X.01.LZZZZZZZ.jpg</ImageUrlLarge>
//    <ListPrice>$8.99</ListPrice>
//    <OurPrice>$8.99</OurPrice>
//    <UsedPrice>$0.50</UsedPrice>
// </Details>
//
//in case there was an error or nothing was found Amazon will return something like:
//<ProductInfo><ErrorMsg>There are no exact matches for the search.</ErrorMsg></ProductInfo>

//global variables:
$totalresults='unknown';

//get the data from amazon
function getAmazon($file) {
  global 
$host;
  global 
$show_xml;
  global 
$totalresults;

  if (
$hf=fopen("http://xml.amazon.com/$file",'r')) {  //open a file from Amazon
    
if ($show_xml) {                                   //print the raw XML instead of parsing it into an array?
      
print "<pre>\n";
    }
    while (
$line=fgets($hf,10000)) {     //handle each line of XML
      
if ($show_xml) {                   //print the raw XML instead of parsing it into an array?
        
print   htmlentities(rtrim($line))."\n";
      }
      if      (
ereg('HTTP/[0-9]\.[0-9] ([13-9].*)$',$line,$Atmp)) {
        if (!
$show_xml) print "HTTP error: ".$Atmp[1]."<br>\n";
      }
      else if (
eregi('<ProductInfo><ErrorMsg>(.*)</ErrorMsg></ProductInfo>',$line,$Atmp)) {
        if (!
$show_xml) print "Amazon search error: ".$Atmp[1]."<br>\n";
      }
      else if (
eregi('<TotalResults>(.*)</TotalResults>',$line,$Atmp)) {
        
$totalresults=$Atmp[1];
      }
      else if (
eregi('<details url="(.*)">',$line,$Atmp)) {
        
$E[url]=$Atmp[1];
      }
      else if (
eregi("<(.*)>(.*)</.*>",$line,$Ares)) {
        
$tag =$Ares[1];
        
$data=$Ares[2];
        if (
$tag=='Author') {  //there can be multiple authors so put in sub array
          
$E[$tag][]=$data;
        }
        else {
          
$E[$tag]=$data;
        }
      }
      else if (
eregi("</details>",$line)) {
        
$A[]=$E;
        
$E=array();  //empty array again
      
}
    }
    if (
$show_xml) {     //print the raw XML instead of parsing it into an array?
      
print "</pre>\n";
    }
  }
  else die(
"Can't open socket for xml.amazon.com.<br>\n");
  return 
$A;
}

//print the array
function printAmazon($A) {
  global 
$associates_id;
  global 
$f_rich;           //do you want 15% or 5%?
  
global $totalresults;

  
//print_array($A);
  
print "<div align='center'>\n";
  print 
"Total number of matches found: $totalresults<br>\n";
  print 
"</div>\n";
  print 
"<hr />\n";
  print 
"<div align='center'>\n";
  print 
"<table width='60%'>\n";
  while (list(
$key,$E)=each($A)) {
    
//print out elements
    
if ($E) {
      
//rebuild the URL if you want 15% instead of 5%:
      
$url=$f_rich?'http://www.amazon.com/exec/obidos/ASIN/'.$E[Asin]."/ref=nosim/".$associates_id:$E[url];
      print 
'<tr>';
      print   
'<td align="center">';
      print     
"<a href='$url'><img src='".$E[ImageUrlMedium]."' border=0></a>";
      print   
'</td>';
      print   
'<td align="center">';
      print     
"<a href='$url'>".$E[ProductName].'</a><br>';
      print     
'by<br>';
      if (
is_array($E[Author])) {
        while (list(
$kau,$dau)=each($E[Author])) {
          if (
$kau) print ', ';
          print 
$dau;
        }
      }
      else print 
'UNKNOWN';
      print     
'<br>';
      print     
'('.$E[OurPrice].')<br>';
      print   
'</td>';
      print 
"</tr>\n";
    }
  }
  print 
"</table>\n";
  print 
"</div>\n";
}

if (
false) {     //change this to true if you want this debugging info to be shown
  
print "<hr>";
  print 
"Debugging:<br>\n";
  print 
"Asking Amazon's XML interface for:<br>\n";
  print 
$file."<br>\n";
  print 
"<hr>";
}

//this is the actual program:
//- it reads the data into an array
//- prints the array
if ($A=getAmazon($file)) {
  
printAmazon($A);
}
else {
  if (!
$show_xml) {
    print(
"No data returned by Amazon.com.");
  }
}

print 
"<hr>\n";
print 
"<ul>\n";
print   
"<li><a href='$PHP_SELF?page=".($page+1)."'>Next page with results    </a></li>\n";
print   
"<li><a href='$PHP_SELF?page=".($page-1)."'>Previous page with results</a></li>\n";
print 
"</ul>\n";
?>