Php Magento 2通过Rest API更新库存
我正在使用此代码通过API更新我的Magento 2股票;但是有一个权限错误,即使凭据是正常的,并且用户角色是“管理员” 有人能帮我吗 错误消息: 字符串(366)” 被禁止的 您没有访问此服务器上的/rest/V1/products/10001001/stockItems/1的权限。 此外,尝试使用ErrorDocument处理请求时遇到403禁止的错误 我的代码是:Php Magento 2通过Rest API更新库存,php,magento,Php,Magento,我正在使用此代码通过API更新我的Magento 2股票;但是有一个权限错误,即使凭据是正常的,并且用户角色是“管理员” 有人能帮我吗 错误消息: 字符串(366)” 被禁止的 您没有访问此服务器上的/rest/V1/products/10001001/stockItems/1的权限。 此外,尝试使用ErrorDocument处理请求时遇到403禁止的错误 我的代码是: <?php $url = "https://www.example.com/"; $token_url=$url."re
<?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);
}
?>
很简单,您没有正确的权限。请确保文件是644,目录是755。确保所有者/组也是正确的(如果您拥有example.com),或者,将其传入用户/密码(如果需要)@thisgyhastwothumbs谢谢您的回答。但在同一文件夹中还有另一个php文件(通过rest api创建产品;使用相同的方法和相同的凭据)正在工作?您检查了文件权限吗?(ls-la
)@Thisgy有两个拇指我在ssh中写了“chown-R me:me public_html”。(意味着,public_html中的所有文件归我所有)…而且所有文件都是644个,文件夹是755个public_html。我知道chown-R
做了什么:)那么实际的文件权限呢?644
?是否仅仅因为您拥有它,并不意味着您的用户执行脚本,可能是apache
,可能是www-data
等等。