caponica / amazon-rainforest
简化访问亚马逊雨林API
Requires
- php: >=5.4.0
- ext-json: *
- guzzlehttp/guzzle: ^6.2 || ^7.4
- psr/log: ^1.0 || ^2.0 || ^3.0
This package is auto-updated.
Last update: 2024-08-29 13:23:00 UTC
README
这是为PHP开发者封装和简化对Rainforest API访问的一次尝试
安装
使用Composer安装
composer require caponica/amazon-rainforest-api
访问API
您的代码将类似于这样
use CaponicaAmazonRainforest\Client\RainforestClient;
use CaponicaAmazonRainforest\Request\ProductRequest;
$config = [ 'api_key' => 'YOUR_API_KEY' ];
$rfClient = new RainforestClient($config);
$rfRequests = [
new ProductRequest(RainforestClient::AMAZON_SITE_USA, 'B001234567'),
// ... can queue up more - they are sent asynchronously ...
];
$rfRequests = $rfClient->prepareRequestArray($requests); // Optional step. De-duplicates the requests and sets keys you can use for local caching.
$apiEntities = $rfClient->retrieveProducts($rfRequests);
foreach ($apiEntities as $key => $rfProduct) {
// do something with the Product object ...
}
API调用返回经过清理的数据和合理的访问器。
与您的代码集成
这个库中的代码使用方法来操作其对象。这意味着您可以扩展Rainforest对象类,然后将您的版本传递给API,它将使用您的版本。
例如:我们在自己的主要代码库中使用Symfony和Doctrine。我们的Doctrine类扩展了Rainforest类
namespace Caponica\OurAmazonBundle\Entity;
class RainforestReview extends \CaponicaAmazonRainforest\Entity\RainforestReview {}
然后我们这样与Rainforest对象一起工作
// ...
$config = [ 'api_key' => 'YOUR_API_KEY' ];
$rfClient = new RainforestClient($config);
$rfRequests = [
new ProductRequest(RainforestClient::AMAZON_SITE_USA, 'B001234567'),
];
$rfRequests = $rfClient->prepareRequestArray($requests); // De-duplicates the requests and sets keys you can use for local caching.
$requestsToSendToApi = [];
$rfEntities = $this->retrieveCachedProductsForMarketAsins(array_keys($requests)); // check to see if we already have local copies of any of the objects we're about to pull from the API
foreach ($requests as $key => $request) {
if (array_key_exists($key, $rfEntities)) {
if ($this->cachedDataIsFresh($rfEntities[$key], $maxAgeHours)) {
$this->logVerbose("$key using cached data");
continue;
} else {
$this->logVerbose("$key cached data stale, fetching from API");
}
} else {
$rfEntities[$key] = new RainforestProduct(); // instantiate our version the Product object for each request
$this->logVerbose("$key not cached, fetching from API");
}
$requestsToSendToApi[$key] = $request;
}
$apiEntities = $rfClient->retrieveProducts($requestsToSendToApi, $rfEntities);
// In the line above we pass our versions of the entities to the API in the second argument.
// It will then update them (and use any overridden methods on our version of the classes)
// instead of creating new ones using its own objects.
foreach ($apiEntities as $key => $rfProduct) {
// do something with the Product object, e.g. persist() it if it's useful ...
}
$this->em->flush(); // Commit the database changes
日志记录
RainforestClient在其构造函数中接受一个可选的Psr\Log\LoggerInterface对象。如果您提供了一个,则可以控制消息的输出(或丢弃)方式。
有关设置日志记录器的更多详细信息,请参阅https://github.com/php-fig/log 和 https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
一个基本的Logger(它简单地回显消息),看起来像这样
# 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实例传递到构造函数中
use CaponicaAmazonRainforest\Client\RainforestClient;
use Your\Path\EchoLogger;
$config = [ ... ];
$echoLogger = new EchoLogger();
$rfClient = new RainforestClient($config, $echoLogger);
关于作者
由Christian Morgan创建和维护。
我是一个经验丰富的亚马逊/电商企业家,也是ScaleForEtail的联合创始人。如果您也是亚马逊品牌所有者或卖家,欢迎加入ScaleForEtail社区。我们为电商社区组织网络研讨会和现场活动。简而言之:我们将志同道合的人聚集在一起。
今天就来看看我们吧! https://facebook.com/groups/scaleforetail/
错误和功能请求
如果您发现某些功能不符合预期,请在本库的GitHub上创建一个问题 - 或者带有修复方案的拉取请求!这个库是开源的,免费提供,不提供任何类型的保证。希望您觉得它很有用!