This wrapper contains the open source PHP SDK that allows you to integrate the Product Advertising API in a larger WordPress project.
Use the following path to include the classes before your code:
require_once WP_PLUGIN_DIR . '/amazon-product-advertising/vendor/autoload.php';
Alternatively use the following action to autoload the classes:
do_action('wp2e_autoload_amazon-product-advertising');
Instantiate a client:
$config = new Configuration();
$config->setAccessKey('YOUR ACCESS KEY');
$config->setSecretKey('YOUR SECRET KEY');
$config->setHost('webservices.amazon.com');
$config->setRegion('us-east-1');
$client = new GuzzleHttp\Client();
$client = new DefaultApi($client,$config);
Search Items:
$partnerTag = 'YOUR PARTNER ID';
$keyword = 'WordPress';
$searchIndex = "Books";
$itemCount = 1;
$resources = [
SearchItemsResource::ITEM_INFOTITLE,
SearchItemsResource::OFFERSLISTINGSPRICE
];
// Forming the request
$searchItemsRequest = new SearchItemsRequest();
$searchItemsRequest->setSearchIndex($searchIndex);
$searchItemsRequest->setKeywords($keyword);
$searchItemsRequest->setItemCount($itemCount);
$searchItemsRequest->setPartnerTag($partnerTag);
$searchItemsRequest->setPartnerType(PartnerType::ASSOCIATES);
$searchItemsRequest->setResources($resources);
// Validating request
$invalidPropertyList = $searchItemsRequest->listInvalidProperties();
$length = count($invalidPropertyList);
if ($length > 0) {
echo "Error forming the request", PHP_EOL;
foreach ($invalidPropertyList as $invalidProperty) {
echo $invalidProperty, PHP_EOL;
}
return;
}
// Sending the request
try {
$searchItemsResponse = $client->searchItems($searchItemsRequest);
echo 'API called successfully', PHP_EOL;
echo 'Complete Response: ', $searchItemsResponse, PHP_EOL;
// Parsing the response
if ($searchItemsResponse->getSearchResult() !== null) {
echo 'Printing first item information in SearchResult:', PHP_EOL;
$item = $searchItemsResponse->getSearchResult()->getItems()[0];
if ($item !== null) {
if ($item->getASIN() !== null) {
echo "ASIN: ", $item->getASIN(), PHP_EOL;
}
if ($item->getDetailPageURL() !== null) {
echo "DetailPageURL: ", $item->getDetailPageURL(), PHP_EOL;
}
if ($item->getItemInfo() !== null
and $item->getItemInfo()->getTitle() !== null
and $item->getItemInfo()->getTitle()->getDisplayValue() !== null) {
echo "Title: ", $item->getItemInfo()->getTitle()->getDisplayValue(), PHP_EOL;
}
if ($item->getOffers() !== null
and $item->getOffers() !== null
and $item->getOffers()->getListings() !== null
and $item->getOffers()->getListings()[0]->getPrice() !== null
and $item->getOffers()->getListings()[0]->getPrice()->getDisplayAmount() !== null) {
echo "Buying price: ", $item->getOffers()->getListings()[0]->getPrice()->getDisplayAmount(), PHP_EOL;
}
}
}
if ($searchItemsResponse->getErrors() !== null) {
echo PHP_EOL, 'Printing Errors:', PHP_EOL, 'Printing first error object from list of errors', PHP_EOL;
echo 'Error code: ', $searchItemsResponse->getErrors()[0]->getCode(), PHP_EOL;
echo 'Error message: ', $searchItemsResponse->getErrors()[0]->getMessage(), PHP_EOL;
}
}
catch (ApiException $exception) {
echo "Error calling PA-API 5.0!", PHP_EOL;
echo "HTTP Status Code: ", $exception->getCode(), PHP_EOL;
echo "Error Message: ", $exception->getMessage(), PHP_EOL;
if ($exception->getResponseObject() instanceof ProductAdvertisingAPIClientException) {
$errors = $exception->getResponseObject()->getErrors();
foreach ($errors as $error) {
echo "Error Type: ", $error->getCode(), PHP_EOL;
echo "Error Message: ", $error->getMessage(), PHP_EOL;
}
}
else {
echo "Error response body: ", $exception->getResponseBody(), PHP_EOL;
}
}
catch (Exception $exception) {
echo "Error Message: ", $exception->getMessage(), PHP_EOL;
}
A Software Development Kit (SDK) is a collection of software development tools in one installable package.
This script is completely standalone and does not do anything after installing it. As such this is a light-weight module for easy inclusion into a bigger project.