PHP code example of ansas / amazon-selling-partner-api
1. Go to this page and download the library: Download ansas/amazon-selling-partner-api library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ansas / amazon-selling-partner-api example snippets
$config = new SellingPartnerApi\Configuration([
"lwaClientId" => "<LWA client ID>",
"lwaClientSecret" => "<LWA client secret>",
"lwaRefreshToken" => "<LWA refresh token>",
"awsAccessKeyId" => "<AWS access key ID>",
"awsSecretAccessKey" => "<AWS secret access key>",
// If you're not working in the North American marketplace, change
// this to another endpoint from lib/Endpoint.php
"endpoint" => SellingPartnerApi\Endpoint::NA,
]);
$config = new SellingPartnerApi\Configuration([
"lwaClientId" => "<LWA client ID>",
"lwaClientSecret" => "<LWA client secret>",
"lwaRefreshToken" => "<LWA refresh token>",
"awsAccessKeyId" => "<AWS access key ID>",
"awsSecretAccessKey" => "<AWS secret access key>",
// If you're not working in the North American marketplace, change
// this to another endpoint from lib/Endpoint.php
"endpoint" => SellingPartnerApi\Endpoint::NA,
"roleArn" => "<Role ARN>",
]);
SellingPartnerApi\Api\SellersV1Api as SellersApi;
use SellingPartnerApi\Configuration;
use SellingPartnerApi\Endpoint;
$config = new Configuration([
"lwaClientId" => "amzn1.application-oa2-client.....",
"lwaClientSecret" => "abcd....",
"lwaRefreshToken" => "Aztr|IwEBI....",
"awsAccessKeyId" => "AKIA....",
"awsSecretAccessKey" => "ABCD....",
// If you're not working in the North American marketplace, change
// this to another endpoint from lib/Endpoint.php
"endpoint" => Endpoint::NA
]);
$api = new SellersApi($config);
try {
$result = $api->getMarketplaceParticipations();
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling SellersApi->getMarketplaceParticipations: ', $e->getMessage(), PHP_EOL;
}
SellingPartnerApi\Configuration;
$config = new Configuration([/* ... */]);
$config->setDebug(true);
// To redirect debug info to a file:
$config->setDebugFile('./debug.log');
use SellingPartnerApi\Api\SellersV1Api as SellersApi;
use SellingPartnerApi\Model\SellersV1 as Sellers;
use SellingPartnerApi\Api\ReportsV20210630Api as ReportsApi;
use SellingPartnerApi\ReportType;
// Assume we've already fetched a report document ID, and that a $config object was defined above
$documentId = 'foo.1234';
$reportType = ReportType::GET_FLAT_FILE_OPEN_LISTINGS_DATA;
$reportsApi = new ReportsApi($config);
$reportDocumentInfo = $reportsApi->getReportDocument($documentId, $reportType['name']);
$docToDownload = new SellingPartnerApi\Document($reportDocumentInfo, $reportType);
$contents = $docToDownload->download(); // The raw report text
/*
* - Array of associative arrays, (each sub array corresponds to a row of the report) if content type is ContentType::TAB or ContentType::CSV
* - A nested associative array (from json_decode) if content type is ContentType::JSON
* - The raw report data if content type is ContentType::PLAIN or ContentType::PDF
* - PHPOffice Spreadsheet object if content type is ContentType::XLSX
* - SimpleXML object if the content type is ContentType::XML
*/
$data = $docToDownload->getData();
// ... do something with report data
// line to replace >>>>$contents = $docToDownload->download(); // The raw report text
$streamContents = $docToDownload->downloadStream(); // The raw report stream
use SellingPartnerApi\Api\FeedsV20210630Api as FeedsApi;
use SellingPartnerApi\FeedType;
use SellingPartnerApi\Model\FeedsV20210630 as Feeds;
$feedType = FeedType::POST_PRODUCT_PRICING_DATA;
$feedsApi = new FeedsApi($config);
// Create feed document
$createFeedDocSpec = new Feeds\CreateFeedDocumentSpecification(['content_type' => $feedType['contentType']]);
$feedDocumentInfo = $feedsApi->createFeedDocument($createFeedDocSpec);
$feedDocumentId = $feedDocumentInfo->getFeedDocumentId();
// Upload feed contents to document
$feedContents = file_get_contents('<your/feed/file.xml>');
// The Document constructor accepts a custom \GuzzleHttp\Client object as an optional 3rd parameter. If that
// parameter is passed, your custom Guzzle client will be used when uploading the feed document contents to Amazon.
$docToUpload = new SellingPartnerApi\Document($feedDocumentInfo, $feedType);
$docToUpload->upload($feedContents);
$createFeedSpec = new Feeds\CreateFeedSpecification();
$createFeedSpec->setMarketplaceIds(['ATVPDKIKX0DER']);
$createFeedSpec->setInputFeedDocumentId($feedDocumentId);
$createFeedSpec->setFeedType($feedType['name']);
$createFeedResult = $feedsApi->createFeed($createFeedSpec);
$feedId = $createFeedResult->getFeedId();
use SellingPartnerApi\Api\FeedsV20210630Api as FeedsApi;
use SellingPartnerApi\FeedType;
$feedType = FeedType::POST_PRODUCT_PRICING_DATA;
$feedsApi = new FeedsApi($config);
// ...
// Create and upload a feed document, and wait for it to finish processing
// ...
$feedId = '1234567890'; // From the createFeed call
$feed = $api->getFeed($feedId);
$feedResultDocumentId = $feed->resultFeedDocumentId;
$feedResultDocument = $api->getFeedDocument($feedResultDocumentId);
$docToDownload = new SellingPartnerApi\Document($feedResultDocument, $feedType);
$contents = $docToDownload->download(); // The raw report data
$data = $docToDownload->getData(); // Parsed/formatted report data