以下是使用 PHP 生成 Amazon 并使用 putListingsItem 上传产品的代码示例:
```php
<?php
// 设置 AWS 访问密钥和卖家 ID
define('AWS_ACCESS_KEY_ID', 'YOUR_ACCESS_KEY_ID');
define('AWS_SECRET_ACCESS_KEY', 'YOUR_SECRET_ACCESS_KEY');
define('MERCHANT_ID', 'YOUR_MERCHANT_ID');
// 设置产品信息
$product = array(
'sku' => 'YOUR_SKU',
'title' => 'YOUR_PRODUCT_TITLE',
'description' => 'YOUR_PRODUCT_DESCRIPTION',
'price' => 'YOUR_PRODUCT_PRICE',
'quantity' => 'YOUR_PRODUCT_QUANTITY',
'condition' => 'YOUR_PRODUCT_CONDITION',
'image' => 'YOUR_PRODUCT_IMAGE_URL',
'category' => 'YOUR_PRODUCT_CATEGORY',
);
// 生成 XML 数据
$xml = '<?xml version="1.0" encoding="utf-8"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
<DocumentVersion>1.01</DocumentVersion>
<MerchantIdentifier>'.MERCHANT_ID.'</MerchantIdentifier>
</Header>
<MessageType>Product</MessageType>
<PurgeAndReplace>false</PurgeAndReplace>
<Message>
<MessageID>1</MessageID>
<OperationType>Update</OperationType>
<Product>
<SKU>'.$product['sku'].'</SKU>
<StandardProductID>
<Type>ASIN</Type>
<Value>'.$product['asin'].'</Value>
</StandardProductID>
<ProductTaxCode>A_GEN_NOTAX</ProductTaxCode>
<DescriptionData>
<Title>'.$product['title'].'</Title>
<Brand>'.$product['brand'].'</Brand>
<Description>'.$product['description'].'</Description>
<BulletPoint>'.$product['bullet_point_1'].'</BulletPoint>
<BulletPoint>'.$product['bullet_point_2'].'</BulletPoint>
<BulletPoint>'.$product['bullet_point_3'].'</BulletPoint>
<BulletPoint>'.$product['bullet_point_4'].'</BulletPoint>
<BulletPoint>'.$product['bullet_point_5'].'</BulletPoint>
<MSRP currency="USD">'.$product['msrp'].'</MSRP>
<Manufacturer>'.$product['manufacturer'].'</Manufacturer>
<ItemType>'.$product['item_type'].'</ItemType>
</DescriptionData>
<ProductData>
<Health>
<ProductType>
<HealthMisc>
<Ingredients>'.$product['ingredients'].'</Ingredients>
</HealthMisc>
</ProductType>
</Health>
</ProductData>
</Product>
</Message>
</AmazonEnvelope>';
// 设置请求参数
$params = array(
'AWSAccessKeyId' => AWS_ACCESS_KEY_ID,
'Action' => 'SubmitFeed',
'Merchant' => MERCHANT_ID,
'SignatureMethod' => 'HmacSHA256',
'SignatureVersion' => '2',
'Timestamp' => gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time()),
'Version' => '2009-01-01',
'FeedType' => '_POST_PRODUCT_DATA_',
'ContentMD5Value' => base64_encode(md5($xml, true)),
);
// 生成签名字符串
ksort($params);
$pairs = array();
foreach ($params as $key => $value) {
array_push($pairs, rawurlencode($key).'='.rawurlencode($value));
}
$canonical_query_string = join('&', $pairs);
$string_to_sign = "POST\nmws.amazonservices.com\n/Feeds/2009-01-01\n".$canonical_query_string;
$signature = rawurlencode(base64_encode(hash_hmac('sha256', $string_to_sign, AWS_SECRET_ACCESS_KEY, true)));
// 发送请求
$url = "https://mws.amazonservices.com/Feeds/2009-01-01?".$canonical_query_string."&Signature=".$signature;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// 处理响应结果
if ($status == 200) {
$xml_response = simplexml_load_string($response);
if ($xml_response->MessageID) {
echo "Product uploaded successfully: ".$xml_response->MessageID;
} else {
echo "Product upload failed";
}
} else {
echo "Error uploading product: ".$response;
}
?>
```
请注意,上述代码仅供参考,需要根据您的实际情况进行修改。同时,您也需要确保您的服务器已经被授权访问 Amazon MWS API,并且您已经在 Amazon 开发者中心注册了您的应用程序。