2014-09-09 4 views
0

판매자가 Amazon Product Ads 서비스에 제품 카탈로그를 추가하는 데 도움이되는 모듈을 구축하고 있습니다. 이를 위해서는 고객 범주와 Amazon Product Ads 범주 간의 매핑을 만들어야합니다.Amazon Product Ads, 카테고리를 검색 할 수있는 API가 있습니까?

문제는 카테고리를 검색하는 데 도움이되는 API 또는 Amazon 제품 광고의 기존 카테고리가 모두 포함 된 파일을 찾을 수 없다는 것입니다.

링크가 http://docs.aws.amazon.com/AWSECommerceService/latest/DG/BrowseNodeIDs.html인데 시작일 수도 있지만 해당 API를 기반으로 텍스트 (예 : '의류')로 검색하는 것이 아니라 NodeId에서 검색 할 수 있습니다.

누구든지 나를 도울 수 있습니까?

당신의 도움이 :) 당신은 하위 범주를 검색 할 수있는 부모의 범주와베이스의 검색 노드 ID를 사용할 필요가

답변

1

주셔서 감사합니다.

<?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; 
}}?> 

<?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라는 이름으로 파일을 작성 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; 
} 
} 
?> 
라는 이름으로 파일을 작성 amazon_api_class.php라는 이름의 파일을 만듭니다 index.php의 공개 키와 비공개 키

+0

와우, 그런 완전한 대답! 고맙습니다! :) –

+0

내 즐거움 ..;) – Upinder

+0

귀하의 구현과 함께, 나는 항상 하나의 주어진 부모 노드에있는 모든 노드를 얻을 것인가? 예를 들어 "옷감"이 텍스트가있는 모든 노드 ID를 "옷감"으로 찾으려면 검색 할 수 없습니까? –