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

时间:2014-09-09 14:05:17

标签: amazon-product-api

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

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

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

你们有人可以帮助我吗?

感谢您的帮助:)

1 个答案:

答案 0 :(得分:1)

您必须使用父类别的浏览节点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中更新您的公钥和私钥