0

I'm trying to get item by SKU using Walmart marketplace API and following this document I'm successful in getting access token but after that when I want to get item by SKU, It fails to get item details. Please help me to find the issue. When I print 'CURLOPT_HTTPGET' it gives response back 80 and if I print '$code' it gives me 401. Seems like I have issues in request header or url but for url I use the official document of Walmart APi. So, if you find any solution, please ping me. Thanks

Here's my code:

function getWalmartDetail()
{
$data = [];
$data['client_id'] = "***********";
$data['client_secret'] = "*************************************";
return $data;
}


function getToken()
{
$data = getWalmartDetail();
$client_id = $data['client_id'];
$client_secret = $data['client_secret'];
$url = "https://marketplace.walmartapis.com/v3/token";
$uniqid = uniqid();
$authorization_key = base64_encode($client_id.":".$client_secret);
$code = "";

$ch = curl_init();
$options = array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 60,
    CURLOPT_HEADER => false,
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => "grant_type=client_credentials",
    CURLOPT_HTTPHEADER => array(
        "WM_SVC.NAME: Walmart Marketplace",
        "WM_QOS.CORRELATION_ID: $uniqid",
        "Authorization: Basic $authorization_key",
        "Accept: application/json",
        "Content-Type: application/x-www-form-urlencoded",
    ),
);
curl_setopt_array($ch,$options);
$response = curl_exec($ch);
$code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);

if($code == 201 || $code == 200)
{
    $token = json_decode($response,true);
    return $token['access_token'];
}

}


 $token = getToken();
 $data = getWalmartDetail();
 $client_id = $data['client_id'];
 $client_secret = $data['client_secret'];
 $authorization = base64_encode($client_id."+".$client_secret);
 $url = "https://marketplace.walmartapis.com/v3/items/1234567?productIdType=SKU";

 $ch = curl_init();
 $qos = uniqid();
 $options = array(
 CURLOPT_URL => $url,
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_TIMEOUT => 60,
 CURLOPT_HEADER => false,
 CURLOPT_HTTPHEADER => array(
    "WM_SVC.NAME: Walmart Marketplace",
    "WM_QOS.CORRELATION_ID: $qos",
    "Authorization: Basic $authorization",
    "WM_SEC_ACCESS_TOKEN: $token",
    "Content-Type: application/json",
    "Accept: application/json",
    ),
    CURLOPT_HTTPGET => true,
    );
    print_r(CURLOPT_HTTPGET);
    curl_setopt_array($ch, $options);
    $response = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    print_r($code);
    curl_close($ch);

    if ($code == 201 || $code == 200){
    $response = json_decode($response,true);
    echo "<pre>";
    print_r("Hey");
    exit();
    } else{
    print_r("No");
    return false;
    }
raobabar
  • 13
  • 2
  • have you tried extracting yourself from the PHP and using something like Postman or insomnia to refine your API requests before moving to code? – Josh Beauregard Aug 26 '21 at 17:50
  • @JoshBeauregard No, I just tried to extract using PHP. Not used Postman or insomnia. – raobabar Aug 27 '21 at 10:09
  • the error does not sound like it lies in your code, it sounds like its it an error in your rest call. checkout one the of the above software to skip the programming while you get that right. (both can auto generate code when your done) – Josh Beauregard Aug 27 '21 at 11:21
  • You're using a '+' instead of ':' when generating the authorization for the get item call. – toastifer Aug 27 '21 at 15:11
  • @toastifer still showing nothing – raobabar Aug 31 '21 at 11:26

0 Answers0