我正在使用此代码通过API更新我的Magento 2库存;但是存在权限错误,即使凭据正常,用户角色也是“管理员”。
有人可以帮助我吗?
错误消息:
string(366)“ 被禁止 您无权访问此服务器上的/ rest / rest / V1 / products / 10001001 / stockItems / 1。 此外,尝试使用ErrorDocument处理请求时遇到403 Forbidden错误。
我的代码:
<?php
$url = "https://www.example.com/";
$token_url=$url."rest/V1/integration/admin/token";
$ch = curl_init();
$data = array("username" => "xxxx", "password" => "xxxx");
$data_string = json_encode($data);
$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$token = curl_exec($ch);
$adminToken= json_decode($token);
$skus = array(
'10001001' => 10,
'10001004' => 8
);
foreach ($skus as $sku => $stock) {
$requestUrl='https://www.example.com/rest/V1/products/' . $sku . '/stockItems/1';
$sampleProductData = array(
'qty' => $stock
);
$productData = json_encode(array('product' => $sampleProductData));
$setHaders = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $requestUrl);
curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
}
?>