0

我正在尝试使用 Walmart 市场 API 按 SKU 获取项目,并按照此文档 成功获取访问令牌,但之后当我想按 SKU 获取项目时,它无法获取项目详细信息。请帮我找出问题所在。当我打印“CURLOPT_HTTPGET”时,它会返回 80,如果我打印“$code”,它会返回 401。似乎我在请求标头或 url 中有问题,但对于 url,我使用 Walmart APi 的官方文档。因此,如果您找到任何解决方案,请联系我。谢谢

这是我的代码:

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

0 回答 0