首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >亚马逊产品广告,是否有搜索类别的API?

亚马逊产品广告,是否有搜索类别的API?
EN

Stack Overflow用户
提问于 2014-09-09 14:05:16
回答 1查看 1.4K关注 0票数 0

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

问题是,我找不到一个API来帮助我搜索类别,也找不到包含亚马逊产品广告所有现有类别的文件。

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

你们谁能帮帮我吗?

(谢谢你的帮助:)

EN

Stack Overflow用户

回答已采纳

发布于 2014-09-10 10:14:12

您必须使用父类别的Browse,并在此基础上搜索子类别。

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

代码语言:javascript
运行
复制
<?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的文件

代码语言:javascript
运行
复制
<?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的文件

代码语言:javascript
运行
复制
<?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中更新您的公钥和私钥

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25746690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档