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;
}