PHP code example of forum-brands / selling-partner-api
1. Go to this page and download the library: Download forum-brands/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/ */
forum-brands / 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;
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 Api\SellersApi($config);
try {
$result = $api->getMarketplaceParticipations();
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling SellersApi->getMarketplaceParticipations: ', $e->getMessage(), PHP_EOL;
}
use SellingPartnerApi\Api\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
use SellingPartnerApi\Api\FeedsApi;
use SellingPartnerApi\FeedType;
use SellingPartnerApi\Model\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);
// ... call FeedsApi::createFeed() with $feedDocumentId
use SellingPartnerApi\Api\FeedsApi;
use SellingPartnerApi\FeedType;
$feedType = FeedType::GET_FLAT_FILE_OPEN_LISTINGS_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->getResultFeedDocumentId();
$feedResultDocument = $api->getFeedDocument($feedResultDocumentId);
$doc = new Document($documentInfo, $feedType);
$docToDownload = new SellingPartnerApi\Document($feedResultDocument, $feedType);
$contents = $docToDownload->download(); // The raw report data
$data = $docToDownload->getData(); // Parsed/formatted report data