PHP code example of lulacanci / oauth2-walmart

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

    

lulacanci / oauth2-walmart example snippets




use Lulacanci\OAuth2\Client\Provider\Walmart;
use Lulacanci\OAuth2\Client\Provider\WalmartMarketplace;

$provider = new Walmart(
    [
        'clientId'     => '{walmart-client-id}',
        'clientSecret' => '{walmart-client-secret}',
    ],
    [],
    WalmartMarketplace::US // or CANADA, MEXICO
);

// Get access token using client credentials
$token = $provider->getAccessTokenWithClientCredentials();

echo 'Access Token: ' . $token->getToken() . "\n";
echo 'Expires in: ' . $token->getExpires() . " seconds\n";

// Use the token with Walmart APIs
// Include it in the WM_SEC.ACCESS_TOKEN header



use Lulacanci\OAuth2\Client\Provider\Walmart;
use Lulacanci\OAuth2\Client\Provider\WalmartMarketplace;

session_start();

$clientId = '{walmart-client-id}';
$clientSecret = '{walmart-client-secret}';
$redirectUri = 'https://example.com/callback-url';

$provider = new Walmart(
    [
        'clientId'     => $clientId,
        'clientSecret' => $clientSecret,
        'redirectUri'  => $redirectUri,
    ],
    [],
    WalmartMarketplace::US
);

if (empty($_GET['code'])) {
    // Step 1: Redirect to Walmart authorization URL
    // The seller will be prompted to log in and authorize your app
    
    $authorizationUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    
    header('Location: ' . $authorizationUrl);
    exit;
    
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
    // State is invalid, possible CSRF attack in progress
    unset($_SESSION['oauth2state']);
    exit('Invalid state');
    
} else {
    // Step 2: Exchange authorization code for access token + refresh token
    
    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code'],
        'redirect_uri' => $redirectUri,
    ]);

    // Store these tokens securely!
    $accessToken = $token->getToken();           // Valid for 15 minutes
    $refreshToken = $token->getRefreshToken();   // Valid for 1 year
    
    echo 'Access Token: ' . $accessToken . "\n";
    echo 'Refresh Token: ' . $refreshToken . "\n";
}



use Lulacanci\OAuth2\Client\Provider\Walmart;
use Lulacanci\OAuth2\Client\Provider\WalmartMarketplace;

$provider = new Walmart([
        'clientId'     => '{walmart-client-id}',
        'clientSecret' => '{walmart-client-secret}',
        'redirectUri'  => 'https://example.com/callback-url',
    ],
    [],
    WalmartMarketplace::US
);

// Use your stored refresh token
$storedRefreshToken = 'your-stored-refresh-token';

$newToken = $provider->getAccessToken('refresh_token', [
    'refresh_token' => $storedRefreshToken,
]);

$accessToken = $newToken->getToken();
// Store the new access token securely

use Lulacanci\OAuth2\Client\Provider\WalmartMarketplace;

// US Marketplace (default)
$provider = new Walmart($options, [], WalmartMarketplace::US);
// Sets clientType=seller

// Canada Marketplace
$provider = new Walmart($options, [], WalmartMarketplace::CANADA);
// Sets clientType=seller-ca

// Mexico Marketplace
$provider = new Walmart($options, [], WalmartMarketplace::MEXICO);
// Sets clientType=seller-mx

$client = new GuzzleHttp\Client();

$response = $client->get('https://marketplace.walmartapis.com/v3/items', [
    'headers' => [
        'WM_SEC.ACCESS_TOKEN' => $token->getToken(),
        'WM_SVC.NAME' => 'Walmart Marketplace',
        'WM_QOS.CORRELATION_ID' => uniqid(),
        'Accept' => 'application/json',
    ],
]);

$authorizationUrl = $provider->getAuthorizationUrl([
    'scope' => ['items', 'orders', 'inventory'],
]);