Location: PHPKode > scripts > Music Albums from Amazon > include/amazon_music_album_api_class.php
<?php
/**
From  : Prabhjit Singh 
Company : Xoriant Solution

Objective : To Get sample music tracks (mp3 files) from the amazon Music Album ASIN.

All results are in JSON Format
**/
?>


<?php    
    require_once 'aws_signed_request.php';
	include_once('amzconfig.php');
	
    class AmazonProductAPI
    {
        /**
         * Your Amazon Access Key Id
         * @access private
         * @var string
         */
        private $public_key     = AMAZON_ACCESS_KEY;
        
        /**
         * Your Amazon Secret Access Key
         * @access private
         * @var string
         */
        private $private_key    = AMAZON_SECRET_KEY;
		       
               				     
        /**
         * Query Amazon with the issued parameters
         * 
         * @param array $parameters parameters to query around
         * @return simpleXmlObject xml query response
         */
        private function queryAmazon($parameters)
        {
            return aws_signed_request("com", $parameters, $this->public_key, $this->private_key);
        }
		
		

		/***************************************Getting Music Tracks ***********************************************************

		/**
         * Return Music Tracks of a product searched by ASIN
         * 
         * @param int $asin_code ASIN code of the product to search
		 * @return json object
         */
        
		
		
		private function getTracks($asin_code)
        {
			$mp3array = array();
            $i = 1;
		           
            do {
              
               $parameters = array("Operation"     => "ItemLookup",
                                "ItemId"        => $asin_code,
				                "RelationshipType" => "Tracks",
				                "RelatedItemPage" => "$i",
				                "ResponseGroup" => "RelatedItems,ItemAttributes");
                                
                $xml_response = $this->queryAmazon($parameters);
        
		    if (isset($xml_response->Items->Item->RelatedItems->RelatedItem)){
  		        foreach($xml_response->Items->Item->RelatedItems->RelatedItem as $items) {
		            if ($items->Item->ItemAttributes->RunningTime > 0) {
					$title = (string)$items->Item->ItemAttributes->Title;
					$mp3asin = (string) $items->Item->ASIN;
					$link =  "http://www.amazon.com/gp/dmusic/media/sample.m3u/ref=dm_mu_dp_trk2_smpl?ie=UTF8&catalogItemType=track&ASIN=$mp3asin&DownloadLocation=CD";
                    $mp3array["$mp3asin"][0] = $title;
					$mp3array["$mp3asin"][1] = $link;
                    }
                }
			}
                 $totalpages= (string) $xml_response->Items->Item->RelatedItems->RelatedItemPageCount;
			     $i++;
		         } while($i <= $totalpages);
			
			if (sizeof($mp3array) > 0){
			   return json_encode($mp3array);
			} else {
               throw new exception("Please Enter ASINs of Music/Album CDs");
			}
 
        }

      /*
	   To get ASIN of Related ITEMS available for an item with diffeerent format and get the desirable  format.
	  */
		private function getRelatedAsins($asin_code)
        {
            $parameters = array("Operation"     => "ItemLookup",
                                "ItemId"        => $asin_code,
				                "RelationshipType" => "AuthorityTitle",
				                "ResponseGroup" => "RelatedItems,ItemAttributes");
                                
            $xml_response = $this->queryAmazon($parameters);
         if (isset($xml_response->Items->Item->RelatedItems->RelatedItem)){
            foreach($xml_response->Items->Item->RelatedItems->RelatedItem as $items) {
		       $binding = (string)$items->Item->ItemAttributes->Binding;
			     if ($binding == "MP3 Download") {
                       $mp3asin = (string) $items->Item->ASIN;
				 }
			 }
         }
			return $mp3asin;
        }
 
        // To Get Parent Asin of All given ASIN to Get Related Formats Available of an Item
        private function getNonbuyableAsin($asin_code)
        {
            $parameters = array("Operation"     => "ItemLookup",
                                "ItemId"        => $asin_code,
				                "RelationshipType" => "AuthorityTitle",
				                "ResponseGroup" => "RelatedItems");
                                
            $xml_response = $this->queryAmazon($parameters);
          
		   return $xml_response->Items->Item->RelatedItems->RelatedItem->Item->ASIN;
		
        } 
        

		//Public Function to Call for getting Music Tracks Preview
        public function GetMusicTracks($asin_code) {
          
			$parentasin = $this->getNonbuyableAsin($asin_code);
            $mp3asin = $this->getRelatedAsins($parentasin);
            return $this->getTracks($mp3asin);
                       
		}

}
?>
Return current item: Music Albums from Amazon