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::seller(
    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.
);

$ordersApi = $connector->ordersV0();
$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::seller(
    clientId: 'amzn1.application-oa2-client.asdfqwertyuiop...',
    clientSecret: 'amzn1.oa2-cs.v1.1234567890asdfghjkl...',
    refreshToken: 'Atzr|IwEBIA...',
    endpoint: Endpoint::NA,
);

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



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,
);


use SellingPartnerApi\SellingPartnerApi;

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

    $amazonWarehousingAndDistributionApi = $sellerConnector->amazonWarehousingAndDistributionV20240509();
    

    $appIntegrationsApi = $sellerConnector->appIntegrationsV20240401();
    

    $applicationManagementApi = $sellerConnector->applicationManagementV20231130();
    

    $aPlusContentApi = $sellerConnector->aPlusContentV20201101();
    

    $catalogItemsApi = $sellerConnector->catalogItemsV20220401();
    

    $catalogItemsApi = $sellerConnector->catalogItemsV20211201();
    

    $catalogItemsApi = $sellerConnector->catalogItemsV0();
    

    $dataKioskApi = $sellerConnector->dataKioskV20231115();
    

    $easyShipApi = $sellerConnector->easyShipV20220323();
    

    $fbaInboundApi = $sellerConnector->fbaInboundV20240320();
    

    $fbaInboundApi = $sellerConnector->fbaInboundV0();
    

    $fbaInboundEligibility = $sellerConnector->fbaInboundEligibilityV1();
    

    $fbaInventoryApi = $sellerConnector->fbaInventoryV1();
    

    $fbaOutboundApi = $sellerConnector->fbaOutboundV20200701();
    

    $feedsApi = $sellerConnector->feedsV20210630();
    

    $financesApi = $sellerConnector->financesV20240619();
    

    $financesApi = $sellerConnector->financesV0();
    

    $invoicesApi = $sellerConnector->invoicesV20240619();
    

    $listingsItemsApi = $sellerConnector->listingsItemsV20210801();
    

    $listingsItemsApi = $sellerConnector->listingsItemsV20200901();
    

    $listingsRestrictionsApi = $sellerConnector->listingsRestrictionsV20210801();
    

    $merchantFulfillmentApi = $sellerConnector->merchantFulfillmentV0();
    

    $messagingApi = $sellerConnector->messagingV1();
    

    $notificationsApi = $sellerConnector->notificationsV1();
    

    $ordersApi = $sellerConnector->ordersV0();
    

    $productFeesApi = $sellerConnector->productFeesV0();
    

    $productPricingApi = $sellerConnector->productPricingV20220501();
    

    $productPricingApi = $sellerConnector->productPricingV0();
    

    $productTypeDefinitionsApi = $sellerConnector->productTypeDefinitionsV20200901();
    

    $replenishmentApi = $sellerConnector->replenishmentV20221107();
    

    $reportsApi = $sellerConnector->reportsV20210630();
    

    $salesApi = $sellerConnector->salesV1();
    

    $sellersApi = $sellerConnector->sellersV1();
    

    $servicesApi = $sellerConnector->servicesV1();
    

    $shipmentInvoicingApi = $sellerConnector->shipmentInvoicingV0();
    

    $shippingApi = $sellerConnector->shippingV2();
    

    $shippingApi = $sellerConnector->shippingV1();
    

    $solicitationsApi = $sellerConnector->solicitationsV1();
    

    $supplySourcesApi = $sellerConnector->supplySourcesV20200701();
    

    $tokensApi = $sellerConnector->tokensV20210301();
    

    $transfersApi = $sellerConnector->transfersV20240601();
    

    $uploadsApi = $sellerConnector->uploadsV20201101();
    


use SellingPartnerApi\SellingPartnerApi;

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

    $directFulfillmentInventoryApi = $vendorConnector->directFulfillmentInventoryV1();
    

    $directFulfillmentOrdersApi = $vendorConnector->directFulfillmentOrdersV20211228();
    

    $directFulfillmentOrdersApi = $vendorConnector->directFulfillmentOrdersV1();
    

    $directFulfillmentPaymentsApi = $vendorConnector->directFulfillmentPaymentsV1();
    

    $directFulfillmentSandboxApi = $vendorConnector->directFulfillmentSandboxV20211028();
    

    $directFulfillmentShippingApi = $vendorConnector->directFulfillmentShippingV20211228();
    

    $directFulfillmentShippingApi = $vendorConnector->directFulfillmentShippingV1();
    

    $directFulfillmentTransactionsApi = $vendorConnector->directFulfillmentTransactionsV20211228();
    

    $directFulfillmentTransactionsApi = $vendorConnector->directFulfillmentTransactionsV1();
    

    $invoicesApi = $vendorConnector->invoicesV1();
    

    $ordersApi = $vendorConnector->ordersV1();
    

    $shipmentsApi = $vendorConnector->shipmentsV1();
    

    $transactionStatusApi = $vendorConnector->transactionStatusV1();
    

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::seller(/* ... */);
$response = $connector->reportsV20210630()->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::seller(/* ... */);
$feedsApi = $connector->feedsV20210630();

// 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::seller(/* ... */);
$response = $connector->feedsV20210630()->getFeedDocument($documentId);
$feedDocument = $response->dto();

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

use SellingPartnerApi\Enums\Marketplace;
use SellingPartnerApi\OAuth;

$oauth = new OAuth(
    clientId: 'amzn1.application-oa2-client.asdfqwertyuiop...',
    clientSecret: 'amzn1.oa2-cs.v1.1234567890asdfghjkl...',
    redirectUri: 'https://example.com/redirect',
);

$authUrl = $oauth->getAuthorizationUri(
    appId: 'amzn1.sp.solution...',
    state: 'unique-base64-encoded-string',
    // The marketplace that you want to authorize the seller in
    marketplace: Marketplace::US,
    // If your app is published on the Marketplace Appstore, pass this parameter:
    // draftApp: false,
);

// Redirect your user to $authUrl...

use SellingPartnerApi\OAuth;

// Parse query parameters from inbound Amazon request...
$authCode = $query['spapi_oauth_code'];

$oauth = new OAuth(
    clientId: 'amzn1.application-oa2-client.asdfqwertyuiop...',
    clientSecret: 'amzn1.oa2-cs.v1.1234567890asdfghjkl...',
    redirectUri: 'https://example.com/redirect',
);

$refreshToken = $oauth->getRefreshToken($authCode);