PHP code example of jlevers / selling-partner-api

1. Go to this page and download the library: Download jlevers/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/ */

    

jlevers / selling-partner-api example snippets


use SellingPartnerApi\SellingPartnerApi;
use SellingPartnerApi\Enums\Endpoint;

$connector = SellingPartnerApi::make(
    clientId: 'amzn1.application-oa2-client.asdfqwertyuiop...',
    clientSecret: 'amzn1.oa2-cs.v1.1234567890asdfghjkl...',
    refreshToken: 'Atzr|IwEBIA...',
    endpoint: Endpoint::NA,  // Or Endpoint::EU, Endpoint::FE, Endpoint::NA_SANDBOX, etc.
)->seller();

$ordersApi = $connector->orders();
$response = $ordersApi->getOrders(
    createdAfter: new DateTime('2024-01-01'),
    marketplaceIds: ['ATVPDKIKX0DER'],
);

$dto = $response->dto();
$purchaseDate = $dto->payload->orders[0]->purchaseDate;

use SellingPartnerApi\SellingPartnerApi;

$connector = SellingPartnerApi::make(
    clientId: 'amzn1.application-oa2-client.asdfqwertyuiop...',
    clientSecret: 'amzn1.oa2-cs.v1.1234567890asdfghjkl...',
    refreshToken: 'Atzr|IwEBIA...',
    endpoint: Endpoint::NA,
)->seller();

$connector->debugRequest(
    function (PendingRequest $pendingRequest, RequestInterface $psrRequest) {
        dd($pendingRequest->headers()->all(), $psrRequest);
    }
);

use SellingPartnerApi\Api\SellersV1Api as SellersApi;
use SellingPartnerApi\Model\SellersV1 as Sellers;


use SellingPartnerApi\SellingPartnerApi;

$sellerConnector = SellingPartnerApi::make(/* ... */)->seller();

    $applicationManagementApi = $sellerConnector->applicationManagement();
    

    $aPlusContentApi = $sellerConnector->aPlusContent();
    

    $authorizationApi = $sellerConnector->authorization();
    

    $catalogItemsApi = $sellerConnector->catalogItems();
    

    $catalogItemsApi = $sellerConnector->catalogItemsV20211201();
    

    $catalogItemsApi = $sellerConnector->catalogItemsV0();
    

    $dataKioskApi = $sellerConnector->dataKiosk();
    

    $easyShipApi = $sellerConnector->easyShip();
    

    $fbaInboundApi = $sellerConnector->fbaInbound();
    

    $fbaInboundEligibility = $sellerConnector->fbaInboundEligibility();
    

    $fbaInventoryApi = $sellerConnector->fbaInventory();
    

    $fbaOutboundApi = $sellerConnector->fbaOutbound();
    

    $fbaSmallAndLightApi = $sellerConnector->fbaSmallAndLight();
    

    $feedsApi = $sellerConnector->feeds();
    

    $financesApi = $sellerConnector->finances();
    

    $listingsItemsApi = $sellerConnector->listingsItems();
    

    $listingsItemsApi = $sellerConnector->listingsItemsV20200901();
    

    $listingsRestrictionsApi = $sellerConnector->listingsRestrictions();
    

    $merchantFulfillmentApi = $sellerConnector->merchantFulfillment();
    

    $messagingApi = $sellerConnector->messaging();
    

    $notificationsApi = $sellerConnector->notifications();
    

    $ordersApi = $sellerConnector->orders();
    

    $productFeesApi = $sellerConnector->productFees();
    

    $productPricingApi = $sellerConnector->productPricing();
    

    $productPricingApi = $sellerConnector->productPricingV0();
    

    $productTypeDefinitionsApi = $sellerConnector->productTypeDefinitions();
    

    $replenishmentApi = $sellerConnector->replenishment();
    

    $reportsApi = $sellerConnector->reports();
    

    $salesApi = $sellerConnector->sales();
    

    $sellersApi = $sellerConnector->sellers();
    

    $servicesApi = $sellerConnector->services();
    

    $shipmentInvoicingApi = $sellerConnector->shipmentInvoicing();
    

    $shippingApi = $sellerConnector->shipping();
    

    $shippingApi = $sellerConnector->shippingV1();
    

    $solicitationsApi = $sellerConnector->solicitations();
    

    $supplySourcesApi = $sellerConnector->supplySources();
    

    $tokensApi = $sellerConnector->tokens();
    

    $uploadsApi = $sellerConnector->uploads();
    


use SellingPartnerApi\SellingPartnerApi;

$vendorConnector = SellingPartnerApi::make(/* ... */)->vendor();

    $directFulfillmentInventoryApi = $vendorConnector->directFulfillmentInventory();
    

    $directFulfillmentOrdersApi = $vendorConnector->directFulfillmentOrders();
    

    $directFulfillmentOrdersApi = $vendorConnector->directFulfillmentOrdersV1();
    

    $directFulfillmentPaymentsApi = $vendorConnector->directFulfillmentPayments();
    

    $directFulfillmentSandboxApi = $vendorConnector->directFulfillmentSandbox();
    

    $directFulfillmentShippingApi = $vendorConnector->directFulfillmentShipping();
    

    $directFulfillmentShippingApi = $vendorConnector->directFulfillmentShippingV1();
    

    $directFulfillmentTransactionsApi = $vendorConnector->directFulfillmentTransactions();
    

    $directFulfillmentTransactionsApi = $vendorConnector->directFulfillmentTransactionsV1();
    

    $invoicesApi = $vendorConnector->invoices();
    

    $ordersApi = $vendorConnector->orders();
    

    $shipmentsApi = $vendorConnector->shipments();
    

    $transactionStatusApi = $vendorConnector->transactionStatus();
    

use SellingPartnerApi\SellingPartnerApi;

$reportType = 'GET_MERCHANT_LISTINGS_DATA';
// Assume we already got a report document ID from a previous getReport call
$documentId = '1234567890.asdf';

$connector = SellingPartnerApi::make(/* ... */)->seller();
$response = $connector->reports()->getReportDocument($documentId, $reportType);

$reportDocument = $response->dto();

/*
 * - Array of arrays, where each sub array corresponds to a row of the report, if this is a TSV, CSV, or XLSX report
 * - A nested associative array (from json_decode) if this is a JSON report
 * - The raw report data if this is a TXT or PDF report
 * - A SimpleXMLElement object if this is an XML report
 */
$contents = $reportDocument->download($reportType);

$streamContents = $reportDocument->downloadStream();  // The raw report stream

use SellingPartnerApi\Seller\FeedsV20210630\Dto\CreateFeedDocumentSpecification;
use SellingPartnerApi\Seller\FeedsV20210630\Dto\CreateFeedSpecification;
use SellingPartnerApi\Seller\FeedsV20210630\Responses\CreateFeedDocumentResponse;

$feedType = 'POST_PRODUCT_PRICING_DATA';

$connector = SellingPartnerApi::make(/* ... */)->seller();
$feedsApi = $connector->feeds();

// Create feed document
$contentType = CreateFeedDocumentResponse::getContentType($feedType);
$createFeedDoc = new CreateFeedDocumentSpecification($contentType);
$createDocumentResponse = $feedsApi->createFeedDocument($createFeedDoc);
$feedDocument = $createDocumentResponse->dto();

// Upload feed contents to document
$feedContents = file_get_contents('your/feed/file.xml');
$feedDocument->upload($feedType, $feedContents);

$createFeedSpec = new CreateFeedSpecification(
    marketplaceIds: ['ATVPDKIKX0DER'],
    inputFeedDocumentId: $feedDocument->feedDocumentId,
    feedType: $feedType,
);

// Create feed with the feed document we just uploaded
$createFeedResponse = $feedsApi->createFeed($createFeedSpec);
$feedId = $createFeedResponse->dto()->feedId;

use SellingPartnerApi\SellingPartnerApi;

$feedType = 'POST_PRODUCT_PRICING_DATA';
// Assume we already got a feed result document ID from a previous getFeed call
$documentId = '1234567890.asdf';

$connector = SellingPartnerApi::make(/* ... */)->seller();
$response = $connector->feeds()->getFeedDocument($documentId);
$feedDocument = $response->dto();

$contents = $feedResultDocument->download($feedType);



use SellingPartnerApi\Seller\OrdersV0\Dto;
use SellingPartnerApi\SellingPartnerApi;

$confirmShipmentRequest = new Dto\ConfirmShipmentRequest(
    packageDetail: new Dto\PackageDetail(
        packageReferenceId: 'PKG123',
        carrierCode: 'USPS',
        trackingNumber: 'ASDF1234567890',
        shipDate: new DateTime('2024-01-01 12:00:00'),
        orderItems: [
            new Dto\ConfirmShipmentOrderItem(
                orderItemId: '1234567890',
                quantity: 1,
            ),
            new Dto\ConfirmShipmentOrderItem(
                orderItemId: '0987654321',
                quantity: 2,
            )
        ],
    ),
    marketplaceId: 'ATVPDKIKX0DER',
);

$response = $ordersApi->confirmShipment(
    orderId: '123-4567890-1234567',
    confirmShipmentRequest: $confirmShipmentRequest,
)