caponica / amazon-mws-complete
完整的Amazon MWS PHP SDK,包括所有库
Requires
- php: >=5.3.2
This package is auto-updated.
Last update: 2024-08-29 03:47:37 UTC
README
这是一个尝试通过单个包简化对MWS API调用全部范围的访问。
安装
到Symfony项目
将引用添加到您的composer.json文件中
"caponica/amazon-mws-complete": "dev-master"
在控制器中使用
$client = new \MwsProductClient(/* args */);
访问API
如果您想的话,可以直接访问每个API
use CaponicaAmazonMwsComplete\AmazonClient\MwsProductClient;
$mwsProductClientUsa = new MwsProductClient(
'YOUR_ACCESS_KEY',
'YOUR_SECRET_KEY',
'YOUR_APP_NAME',
'YOUR_APP_VERSION',
[ 'ServiceURL' => 'https://mws.amazonservices.com/Products/2011-10-01' ]
);
$mwsResponse = $mwsProductClientUsa->getCompetitivePricingForASIN([
'SellerId' => 'YOUR_SELLER_ID',
'MarketplaceId' => 'MARKETPLACE_ID',
'ASINList' => array('ASIN' => 'YOUR_ASIN_SEARCH'),
]);
// ... do something with the response ...
然而,还有更好的方法!MwsClientPool封装了所有不同亚马逊API服务之间的共享配置。因此,只需创建一次(针对您想要与之合作的每个市场/卖家组合),然后从池中检索'ClientPacks'。ClientPack包括单个卖家和市场配置,这些可以通过callXyz()方法(例如,使用callGetCompetitivePricingForASIN()调用API的getCompetitivePricingForASIN()方法)预先填充到API调用中。这使得您的API调用更加简洁。
use CaponicaAmazonMwsComplete\ClientPool\MwsClientPool;
use CaponicaAmazonMwsComplete\ClientPool\MwsClientPoolConfig;
$mwsClientPoolUsa = new MwsClientPool();
$mwsClientPoolUsa->setConfig([
'amazon_site' => MwsClientPoolConfig::SITE_US
'access_key' => 'YOUR_ACCESS_KEY'
'secret_key' => 'YOUR_SECRET_KEY'
'application_name' => 'YOUR_APP_NAME'
'application_version' => 'YOUR_APP_VERSION'
'seller_id' => 'YOUR_SELLER_ID'
]);
$productClientPackUsa = $mwsClientPoolUsa->getProductClientPack();
$mwsResponse = $productClientPackUsa->callGetCompetitivePricingForASIN('YOUR_ASIN_SEARCH');
还有一些辅助方法返回对象(或对象数组),与原始MWS响应相比更容易使用。
/** @var MwsCompetitivePricing[] $compPricings */
$compPricings = $productClientPackUsa->retrieveCompetitivePricingForASIN('YOUR_ASIN_SEARCH');
foreach ($compPricings as $compPricing) {
echo $compPricing->asin;
}
MWSAuthToken
如果您正在使用MWSAuthToken,则可以通过配置传入它
$mwsClientPoolUsa->setConfig([
'auth_token' => 'YOUR_MWS_AUTH_TOKEN',
// ... other parameters ...
]);
一旦设置在ClientPool上,该令牌应传递到每个Client,并用于每个API请求。
请求:请通过github提供反馈,如果这是设置和使用MWSAuthToken的最佳方式,以及是否按预期工作。我没有使用此功能,因此无法对其进行正确测试。
日志记录
脚本不再输出消息。相反,它们使用Logger,您可以在实例化ClientPool时定义它。
有关设置Logger的详细信息,请参阅https://github.com/php-fig/log和https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md。
一个基本的Logger(它复制了旧echo行为),如下所示
# EchoLogger.php
namespace Your\Path;
use Psr\Log\AbstractLogger;
class EchoLogger extends AbstractLogger
{
/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
*
* @return void
*/
public function log($level, $message, array $context = array())
{
echo "$message\n";
}
}
然后,您只需将EchoLogger实例传递给MwsClientPool构造函数
use CaponicaAmazonMwsComplete\ClientPool\MwsClientPool;
use Your\Path\EchoLogger;
$echoLogger = new EchoLogger();
$mwsClientPoolUsa = new MwsClientPool($echoLogger);
处理报告
有一些域对象可以帮助读取报告。并非所有报告都已实现(远远没有),但请随意实现您需要的任何报告并提交PR。基本结构是有一个与总体报告相关的ReportXyz
类,以及一个与报告中每个记录(行)相关的ReportXyzRecord
类。
基本使用可能如下所示
$reportFilePath = '/path/to/file.txt';
$reportType = MwsFeedAndReportClientPack::REPORT_FBA_INVENTORY_AFN;
$fileHandle = @fopen($reportFilePath, 'r');
if (!$fileHandle) {
return "Could not open $reportFilePath to read the report from";
}
try {
while (($lineFromFile = fgets($fileHandle)) !== false) {
if (!$checkedHeader) {
BaseMwsReport::validateHeaderRowForReportType($lineFromFile, $reportType);
$reportRecordClass = BaseMwsReport::convertReportTypeToReportRecordClass($reportType);
$checkedHeader = true;
continue;
}
$reportRecord = new $reportRecordClass($lineFromFile);
// do something with the record
echo "The SellerSku field value is" . $reportRecord->getSellerSku();
}
} catch (InvalidReportHeaderException $e) {
// Handle the exception, which is thrown if the header is not the expected format
} catch (InvalidReportRecordException $e) {
// Handle the exception, which is thrown if the row is not the expected format
}
客户端库版本
?? 亚马逊已弃用这些API,因此将来可能会从该包中删除。
*1 这些库以前显示后续库版本(日期),但这是当前Client.php文件中的内容
目前不包括Off-Amazon Payments API,因为它与MWS活动没有直接关联。Off-Amazon Payments技术参考