2016-01-12 91 views
1


我正在使用PHP類與Amazon產品廣告API進行交互。我需要能夠查找書籍(ISBN編號),但是這個類似乎不支持它。我試圖修改關鍵字功能以接受ISBN號碼,但是我沒有運氣。任何人都可以將我指向資源的正確方向來學習如何做到這一點,或者如果這是一個簡單的解決方案展示給我看?謝謝你在前進
如何更改此亞馬遜產品廣告API PHP連接以搜索ISBN

amazon_api_class.php 

<?php 
    require_once 'aws_signed_request.php'; 

    class AmazonProductAPI 
    { 

     private $public_key  = "YOUR AMAZON ACCESS KEY ID"; 
     private $private_key = "YOUR AMAZON SECRET KEY"; 

     /* 'Associate Tag' now required, effective from 25th Oct. 2011 */ 
     private $associate_tag = "YOUR AMAZON ASSOCIATE TAG"; 

     const MUSIC = "Music"; 
     const DVD = "DVD"; 
     const GAMES = "VideoGames"; 

     private function verifyXmlResponse($response) 
     { 
      if ($response === False) 
      { 
       throw new Exception("Could not connect to Amazon"); 
      } 
      else 
      { 
       if (isset($response->Items->Item->ItemAttributes->Title)) 
       { 
        return ($response); 
       } 
       else 
       { 
        throw new Exception("Invalid xml response."); 
       } 
      } 
     } 

     private function queryAmazon($parameters) 
     { 
      return aws_signed_request("com", 
             $parameters, 
             $this->public_key, 
             $this->private_key, 
             $this->associate_tag); 
     } 

     public function searchProducts($search,$category,$searchType="UPC") 
     { 
      $allowedTypes = array("UPC", "TITLE", "ARTIST", "KEYWORD"); 
      $allowedCategories = array("Music", "DVD", "VideoGames"); 

      switch($searchType) 
      { 
       case "UPC" : 
        $parameters = array("Operation"  => "ItemLookup", 
             "ItemId"  => $search, 
             "SearchIndex" => $category, 
             "IdType"  => "UPC", 
             "ResponseGroup" => "Medium"); 
           break; 

       case "TITLE" : 
        $parameters = array("Operation"  => "ItemSearch", 
             "Title"   => $search, 
             "SearchIndex" => $category, 
             "ResponseGroup" => "Medium"); 
           break; 

      } 

      $xml_response = $this->queryAmazon($parameters); 

      return $this->verifyXmlResponse($xml_response); 

     } 

     public function getItemByUpc($upc_code, $product_type) 
     { 
      $parameters = array("Operation"  => "ItemLookup", 
           "ItemId"  => $upc_code, 
           "SearchIndex" => $product_type, 
           "IdType"  => "UPC", 
           "ResponseGroup" => "Medium"); 

      $xml_response = $this->queryAmazon($parameters); 

      return $this->verifyXmlResponse($xml_response); 

     } 

     public function getItemByAsin($asin_code) 
     { 
      $parameters = array("Operation"  => "ItemLookup", 
           "ItemId"  => $asin_code, 
           "ResponseGroup" => "Medium"); 

      $xml_response = $this->queryAmazon($parameters); 

      return $this->verifyXmlResponse($xml_response); 
     } 

     public function getItemByKeyword($keyword, $product_type) 
     { 
      $parameters = array("Operation" => "ItemSearch", 
           "Keywords" => $keyword, 
           "SearchIndex" => $product_type); 

      $xml_response = $this->queryAmazon($parameters); 

      return $this->verifyXmlResponse($xml_response); 
     } 

    } 

    ?> 

回答

0

Lookup by ISBN需要設置這裏的idtype參數書號。擴展您的代碼可能如下所示:

public function getItemByISBN($isbn) 
    { 
     $parameters = array("Operation"  => "ItemLookup", 
          "ItemId"  => $isbn, 
          "IdType"  => "ISBN" 
          "ResponseGroup" => "Medium"); 

     $xml_response = $this->queryAmazon($parameters); 

     return $this->verifyXmlResponse($xml_response); 
    } 
相關問題