如何在php中使用AWS api获取亚马逊产品的产品库存?

时间:2017-08-03 05:40:03

标签: php amazon-product-api

我使用以下代码获取amazon产品数据,所有产品都使用xml,但xml内容中没有产品库存

任何人都可以指导我如何获取产品库存信息。



<?php 
define('AWS_ACCESS_KEY_ID', 'my-access-key'); 
define('AWS_SECRET_ACCESS_KEY', 'my-secret-key'); 
define('AMAZON_ASSOC_TAG', 'my-associate-tag'); 

function amazon_get_signed_url($searchTerm) 
{ 
    $base_url = "http://ecs.amazonaws.com/onca/xml"; 
    $params = array( 'AWSAccessKeyId' => AWS_ACCESS_KEY_ID, 'AssociateTag' => AMAZON_ASSOC_TAG, 'Version' => "2010-11-01", 'Operation' => "ItemLookup", 'Service' => "AWSECommerceService",  'ResponseGroup' => "ItemAttributes", 'ItemId'=> $searchTerm);

if(empty($params['AssociateTag'])) 
{ 
 unset($params['AssociateTag']); 
} 

// Add the Timestamp 
$params['Timestamp'] = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time()); 

// Sort the URL parameters 
$url_parts = array(); 
foreach(array_keys($params) as $key) 
$url_parts[] = $key . "=" . str_replace('%7E', '~', rawurlencode($params[$key])); 
sort($url_parts); 
// Construct the string to sign 
$url_string = implode("&", $url_parts); 
$string_to_sign = "GET\necs.amazonaws.com\n/onca/xml\n" . $url_string; 
// Sign the request 
$signature = hash_hmac("sha256", $string_to_sign, AWS_SECRET_ACCESS_KEY, TRUE);
 // Base64 encode the signature and make it URL safe 
 $signature = urlencode(base64_encode($signature)); 
 $url = $base_url . '?' . $url_string . "&Signature=" . $signature; 
 return ($url); 
}

$getthis = 'B004XIE6WI'; /*---- Product ASIN-----*/
$show = amazon_get_signed_url($getthis);

$ch = curl_init($show); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 15); 
$c = curl_exec($ch); 
$xml = simplexml_load_string($c);
$json = json_encode($xml); 
$array = json_decode($json,TRUE);
echo "<pre>";
print_r($array);
echo "</pre>";
?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

Amazon Product Api不提供库存信息。这里也提出了类似的问题:How to get remaining stock from Product Advertising API (AWS)