Location: PHPKode > scripts > AmazonAPI > amazonapi/class.AmazonAPI.inc
<?php
/*
    -
    AmazonAPI ver 1.0.0
    Author: Daniel Kushner
    Email: hide@address.com
    Release: 20 July, 2002
    http://www.amazonlite.com/
  
*/

class AmazonAPI {

    var $modes = array('books'          => array('name'          => 'Books',
                                                 'topselling'    => '1000'),
                       'classical'      => array('name'          => 'Classical Music',
                                                 'topselling'    => '85'),
                       'dvd'            => array('name'          => 'DVD',
                                                 'topselling'    => '286723'),                                         
        			   'music'          => array('name'          => 'Popular Music',
                                                 'topselling'    => '301668'),
                       'electronics'    => array('name'          => 'Electronics',
                                                 'topselling'    => '172282'),
                       'pc-hardware'    => array('name'          => 'Computers',
                                                 'topselling'    => '565118'),
                       'software'       => array('name'          => 'Software',
                                                 'topselling'    => '491286'),
                       'videogames'     => array('name'          => 'Computer & Video Games',
                                                 'topselling'    => '471280'),
                       'garden'         => array('name'          => 'Outdoor Living',
                                                 'topselling'    => '468250'),
                       'kitchen'        => array('name'          => 'Kitchen & Housewares',
                                                 'topselling'    => '491864'),                                         
        			   'universal'      => array('name'          => 'Tools & Hardware',
                                                 'topselling'    => '468240'),
                       'vhs'            => array('name'          => 'Video',
                                                 'topselling'    => '286721'),
                       'baby'           => array('name'          => 'Baby',
                                                 'topselling'    => '542456'),
                       'magazines'      => array('name'          => 'Magazines',
                                                 'topselling'    => '599872'),
                       'photo'          => array('name'          => 'Camera & Photo',
                                                 'topselling'    => '513080'),                                         
        			   'toys'           => array('name'          => 'Toys & Games',
                                                 'topselling'    => '491290'));
                                                 
    var $developersToken    = 'D3AQP3D19SLFXA';
    var $associate          = 'websapp';
    var $searchType         = 'heavy';
    var $amazonURL          = 'xml.amazon.com';
    var $amazonURI          = '/onca/xml';
    var $defaultMode        = 'books';
       
    function AmazonAPI($searchType = 'heavy') {
        $this->setSearchType($searchType);
    }
    
    function setSearchType($searchType) {
        $this->searchType = $searchType;
    }
    
    function isMode($mode) {
        return isset($this->modes[$mode]);
    }
    
    function getModes() {
        return array_keys($this->modes);    
    }
    
    function getDefaultMode() {
        return $this->defaultMode;
    }
    
    function getModeName($mode) {
        return $this->isMode($mode) ? $this->modes[$mode]['name'] : false;
    }
    
    function keywordSearch($input, $mode, $page=1) {
        return $this->_genericSearch("KeywordSearch=$input&mode=$mode", $page);
    }
    
    function browseNodeSearch($browseNode, $mode, $page = 1) {
        return $this->_genericSearch("BrowseNodeSearch=$browseNode&mode=$mode", $page);
    }
    
    function asinSearch($asin) {
        if(is_array($asin)) {
            $asin = implode($asin, ',');
        }
        return $this->_genericSearch("AsinSearch=$asin");
    }
    
    function isbnSearch($isbn) {
        return $this->asinSearch($isbn);
    }
    
    function upcSearch($upc, $mode) {
        if(is_array($upc)) {
            $upc = implode($upc, ',');
        }
        if(!in_array($mode, array('music', 'classical'))) {
            return false;
        }
        return $this->_genericSearch("UpcSearch=$upc&mode=$mode", $page);
    }
    
    function authorSearch($author, $page = 1) {
        return $this->_genericSearch("AuthorSearch=$author&mode=books", $page);
    }
    
    function artistSearch($artist, $mode, $page = 1) {
        if(!in_array($mode, array('music', 'classical'))) {
            return false;
        }
        return $this->_genericSearch("ArtistSearch=$artist&mode=$mode", $page);
    }
    
    function actorSearch($actor, $mode, $page = 1) {
        if(!in_array($mode, array('dvd', 'vhs', 'video'))) {
            return false;
        }
        return $this->_genericSearch("ActorSearch=$actor&mode=$mode", $page);
    }
    
    function directorSearch($director, $mode, $page = 1) {
        if(!in_array($mode, array('dvd', 'vhs', 'video'))) {
            return false;
        }
        return $this->_genericSearch("DirectorSearch=$director&mode=$mode", $page);
    }
    
    function manufacturerSearch($manufacturer, $mode, $page = 1) {
        if(!in_array($mode, array('electronics', 'kitchen', 'videogames', 'software', 'photo', 'pc-hardware'))) {
            return false;
        }
        return $this->_genericSearch("ManufacturerSearch=$manufacturer&mode=$mode", $page);
    }
    
    function listManiaSearch($listMania, $page = 1) {
        return $this->_genericSearch("ListManiaSearch=$listMania", $page);
    }
    
    function similaritySearch($asin, $page = 1) {
        return $this->_genericSearch("SimilaritySearch=$asin", $page);
    }
    
       
    
    
    function _genericSearch($append, $page = 1) {
        return $this->_getSearchResult("?v=1.0&dev-t={$this->developersToken}&t={$this->associate}&type={$this->searchType}&page=$page&f=xml&$append");
    }
    
    function _getSearchResult($searchString) {
        //var_dump($searchString);
        //echo'<br>';
        $searchString = urlencode($searchString);
        $fp = fsockopen($this->amazonURL, 80);
        fputs($fp, "GET ".$this->amazonURI.$searchString." HTTP/1.0\n\n");
        $body = false;
        $xml = '';
        while(($line = fgets($fp, 1024))) {
        	$body = $body || trim($line) == "";
        	if($body) {
        		$xml .= $line;
        	}
        }
        fclose($fp);
        return trim($xml);
    }
    
    
}
?>
Return current item: AmazonAPI