PHP code example of highsidelabs / amazon-business-api

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

    

highsidelabs / amazon-business-api example snippets


$config = new AmazonBusinessApi\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' => AmazonBusinessApi\Endpoint::NA,
]);

$config = new AmazonBusinessApi\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' => AmazonBusinessApi\Endpoint::NA,
    'roleArn' => '<Role ARN>',
]);


AmazonBusinessApi\Api\ProductSearchV20200826Api as ProductSearchApi;
use AmazonBusinessApi\Configuration;
use AmazonBusinessApi\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 ProductSearchApi($config);
try {
    $result = $api->productsRequest('B0B96H7LGX', 'US', 'en_US', '[email protected]');
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling ProductSearchApi->productsRequest: ', $e->getMessage(), PHP_EOL;
}



AmazonBusinessApi\Configuration;

$config = new Configuration([/* ... */]);
$config->setDebug(true);
// To redirect debug info to a file:
$config->setDebugFile('./debug.log');

use AmazonBusinessApi\Api\ProductSearchV20200826Api as ProductSearchApi;
use AmazonBusinessApi\Model\ProductSearchV20200826 as ProductSearch;

$accountHolder = new AmazonBusinessApi\Model\UserManagementV20210830Api\AccountHolder([
    'email' => '[email protected]',
    'given_name' => 'Jane',
    'family_name' => 'Doe'
]);

$accountHolder = new AmazonBusinessApi\Model\UserManagementV20210830Api\AccountHolder();
$accountHolder->email = '[email protected]';
$accountHolder->givenName = 'Jane';
$accountHolder->familyName = 'Doe';

$accountHolder->email;          // -> '[email protected]'
$accountHolder->givenName;      // -> 'Jane'
$accountHolder->familyName;     // -> 'Doe'

use AmazonBusinessApi\Model\OrderingV1;

$placeOrderRequest = new OrderingV1\PlaceOrderRequest([
    // ...
    'line_items' => [
        new OrderingV1\RequestLineItem([
            'quantity' => 1,
            // ...
        ]),
        new OrderingV1\RequestLineItem([
            'quantity' => 2,
            // ...
        ]),
    ],
    // ...
]);


AmazonBusinessApi\Api\ProductSearchV20200826Api as ProductSearchApi;
use AmazonBusinessApi\Configuration;
use AmazonBusinessApi\Endpoint;

$config = new Configuration([...]);
$api = new ProductSearchApi($config);
try {
    $result = $api->productsRequest('B0B96H7LGX', 'US', 'en_US', '[email protected]');
    $headers = $result->headers;
    print_r($headers);
} catch (Exception $e) {
    echo 'Exception when calling ProductSearchApi->productsRequest: ', $e->getMessage(), PHP_EOL;
}

// CustomAuthorizationSigner.php
use Psr\Http\Message\RequestInterface;
use AmazonBusinessApi\Contract\AuthorizationSignerContract;

class CustomAuthorizationSigner implements AuthorizationSignerContract
{
    public function sign(RequestInterface $request, Credentials $credentials): RequestInterface
    {
        // Calculate request signature and request date.
        
        $requestDate = '20220426T202300Z';
        $signatureHeaderValue = 'some calculated signature value';
        
        $signedRequest = $request
            ->withHeader('Authorization', $signatureHeaderValue)
            ->withHeader('x-amz-date', $requestDate);
        
        return $signedRequest;
    }

    // ...
}

// Consumer code


// RemoteRequestSigner.php
use Psr\Http\Message\RequestInterface;
use AmazonBusinessApi\Contract\RequestSignerContract;

class RemoteRequestSigner implements RequestSignerContract
{
    public function signRequest(RequestInterface $request): RequestInterface {
        // Sign request by sending HTTP call
        // to external/separate service instance.
        
        return $signedRequest;
    }
}

// Consumer code

(Exception $e) {
    echo 'Exception when calling ProductSearchApi->productsRequest: ', $e->getMessage(), PHP_EOL;
}
 php
$requestBody = new AmazonBusinessApi\Model\UserManagementV20210830Api\CreateBusinessUserAccountRequest([
    // ...
    'account_holder' => $accountHolder,
    // ...
]);

$requestBody->accountHolder;        // -> [AccountHolder instance]
$requestBody->requestBody->email;  // -> '[email protected]'