【问题标题】:Amazon Product Ads, is there an API to search for Categories?亚马逊产品广告,是否有用于搜索类别的 API?
【发布时间】:2014-09-10 18:14:12
【问题描述】:

我正在构建一个模块,帮助卖家将他们的产品目录放入亚马逊产品广告服务中。为此,我需要在客户的类别和亚马逊产品广告类别之间进行映射。

问题是,我找不到可以帮助我搜索类别的 API,或者找不到包含所有现有亚马逊产品广告类别的文件。

我发现链接 http://docs.aws.amazon.com/AWSECommerceService/latest/DG/BrowseNodeIDs.html,这可能是一个开始,但根据他们的 API,不可能通过文本(如“服装”)搜索,而是通过 NodeId 搜索,这不是我想要的: /

请问有谁能帮帮我吗?

感谢您的帮助:)

【问题讨论】:

    标签: amazon-product-api


    【解决方案1】:

    您必须使用父类别的浏览节点 ID,并根据该 ID 搜索子类别。

    创建一个名为 amazon_api_class.php 的文件

    <?php
    
    require_once 'aws_signed_request.php';
    
    class AmazonProductAPI
    {
    /**
     * Your Amazon Access Key Id
     * @access private
     * @var string
     */
    private $public_key     = "";
    
    /**
     * Your Amazon Secret Access Key
     * @access private
     * @var string
     */
    private $private_key    = "";
    
    private $media_type     = "";
    
    private $region         = "";
    
    private $out_file_fp    = "";
    
    
    public function __construct($public, $private, $region) {
        $this->public_key   = $public;
        $this->private_key  = $private;
        $this->region       = $region;
    }        
    
    public function getNode($node)
    {
        $parameters = array("Operation"  => "BrowseNodeLookup",
                                            "BrowseNodeId"   => $node,
                                            "ResponseGroup" => "BrowseNodeInfo");
    
        $xml_response = aws_signed_request($parameters,
                                           $this->public_key,
                                           $this->private_key,
                                           $this->region);
    
        return ($xml_response);
    }
    
    
    public function setMedia($media, $file = "") {
        $media_type = array("display", "csv");
    
        if(!in_array($media,$media_type)) {
            throw new Exception("Invalid Media Type");
            exit();
        }
    
        $this->media_type = $media;
    
        if($media == "csv") {
            $this->out_file_fp = fopen($file,'a+');
        }
    }
    
    
    private function writeOut($level, $name, $id, $parent) {
        if($this->media_type == "display") {
            $spaces = str_repeat( ' ', ( $level * 6 ) );
            echo $spaces . $parent . ' : ' . $name . ' : ' . $id . "\n";   
        } elseif ($this->media_type == "csv") {
            $csv_line = '"' . $parent . '","' . $name . '","' . $id . '"' . "\n";
            fputs($this->out_file_fp, $csv_line);
        } else {
            throw new Exception("Invalid Media Type");
            exit();
        }
    }
    
    
    public function getBrowseNodes($nodeValue, $level = 0)
    {
        try {
            $result = $this->getNode($nodeValue);
        }
        catch(Exception $e) {
            echo $e->getMessage();
        }
    
        if(!isset($result->BrowseNodes->BrowseNode->Children->BrowseNode)) return;
    
        if(count($result->BrowseNodes->BrowseNode->Children->BrowseNode) > 0) {
            foreach($result->BrowseNodes->BrowseNode->Children->BrowseNode as $node) {
                $this->writeOut($level, $node->Name,
                                $node->BrowseNodeId,
                                $result->BrowseNodes->BrowseNode->Name);
    
                $this->getBrowseNodes($node->BrowseNodeId, $level+1);
            }
        } else {
            return;        
        }
    }
    
    
    public function getNodeName($nodeValue)
    {
        try {
            $result = $this->getNode($nodeValue);
        }
        catch(Exception $e) {
            echo $e->getMessage();
        }
    
        if(!isset($result->BrowseNodes->BrowseNode->Name)) return;
    
        return (string)$result->BrowseNodes->BrowseNode->Name;
    }
    
    
    public function getParentNode($nodeValue)
    {
        try {
            $result = $this->getNode($nodeValue);
        }
        catch(Exception $e) {
            echo $e->getMessage();
        }
    
        if(!isset($result->BrowseNodes->BrowseNode->Ancestors->BrowseNode->BrowseNodeId)) return;
    
        $parent_node = array("id" => (string)$result->BrowseNodes->BrowseNode->Ancestors->BrowseNode->BrowseNodeId,
                             "name" => (string)$result->BrowseNodes->BrowseNode->Ancestors->BrowseNode->Name);
        return $parent_node;
    }}?>
    

    创建名为 aws_signed_request.php 的文件

    <?php
    
    
    function  aws_signed_request($params,$public_key,$private_key,$region)   
    {
    
    $method = "GET";
    $host = "ecs.amazonaws.".$region; // must be in small case
    $uri = "/onca/xml";
    
    
    $params["Service"]          = "AWSECommerceService";
    $params["AWSAccessKeyId"]   = $public_key;
    $params["AssociateTag"]     = 'YOUR-ASSOCIATES-ID-HERE';
    $params["Timestamp"]        = gmdate("Y-m-d\TH:i:s\Z");
    $params["Version"]          = "2009-03-31";
    
    /* The params need to be sorted by the key, as Amazon does this at
      their end and then generates the hash of the same. If the params
      are not in order then the generated hash will be different thus
      failing the authetication process.
    */
    ksort($params);
    
    $canonicalized_query = array();
    
    foreach ($params as $param=>$value)
    {
        $param = str_replace("%7E", "~", rawurlencode($param));
        $value = str_replace("%7E", "~", rawurlencode($value));
        $canonicalized_query[] = $param."=".$value;
    }
    
    $canonicalized_query = implode("&", $canonicalized_query);
    
    $string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query;
    
    /* calculate the signature using HMAC with SHA256 and base64-encoding.
       The 'hash_hmac' function is only available from PHP 5 >= 5.1.2.
    */
    $signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));
    
    /* encode the signature for the request */
    $signature = str_replace("%7E", "~", rawurlencode($signature));
    
    /* create request */
    $request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature;
    
    /* I prefer using CURL */
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$request);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    
    $xml_response = curl_exec($ch);
    
    if ($xml_response === False)
    {
        return False;
    }
    else
    {
        /* parse XML */
        $parsed_xml = @simplexml_load_string($xml_response);
        return ($parsed_xml === False) ? False : $parsed_xml;
    }
    }
    ?>
    

    创建名为 index.php 的文件

    <?php
    
    /* Example usage of the Amazon Product Advertising API */ 
    include("amazon_api_class.php");
    
    $public_key     = "YOUR-AMAZON-PUBLIC-KEY";
    $private_key    = "YOUR-AMAZON-SECRET-KEY";
    $region         = "com"; // or "CA" or "DE" etc.
    
    $obj = new AmazonProductAPI($public_key, $private_key, $region); 
    $obj->setMedia("display");
    $obj->getBrowseNodes("1036592"); //Apparel US store
    
    ?>
    

    不要忘记在 index.php 中更新你的公钥和私钥

    【讨论】:

    • 哇,这么完整的答案!谢谢! :)
    • 通过您的实现,我将始终获得一个给定父节点中的所有节点,对吗?我将无法通过例如“cloth”查找所有节点 ID,其文本包含“cloth”?
    • yup..你可以在操作中使用“Itemlookup”,在响应组中使用“BrowseNodeInfo”以及要搜索的关键字..
    猜你喜欢
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    相关资源
    最近更新 更多