Magento 2 REST API中的PUT方法

时间:2018-06-12 00:56:25

标签: php rest api magento

我不确定;但是我的Magento 2的PUT方法(REST API)存在一个有趣的问题。

我可以用POST方法做我想做的一切。 但我的所有脚本用户PUT,以及我的主题脚本也使用PUT方法肯定不起作用;他们都给出了404错误。

例如我正在尝试使用此脚本进行更新:

<?php

$url = "https://www.example.com/";
$token_url=$url."rest/V1/integration/admin/token";
$ch = curl_init();
$data = array("username" => "xxxxx", "password" => "xxxxx");
$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(
  '10001007' => 10
);

foreach ($skus as $sku => $stock) {
  $sampleProductData = array(
          'qty' => $stock,
  );


$productData = json_encode(array('stockItem' => $sampleProductData));
$setHaders = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.example.com/index.php/rest/V1/products/10001007/stockItems/1");
curl_setopt($ch, CURLOPT_POSTFIELDS, $productData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);


$response = curl_exec($ch);

echo curl_error($ch);
curl_close($ch);


    unset($productData);
    unset($sampleProductData);

  var_dump($response);
}
?>

这是一个基本的脚本;但是返回404错误。

我尝试了每个API网址; 如:

https://www.example.com/index.php/rest/V1/products/10001007/stockItems/1?store_id=1

https://www.example.com/rest/all/V1/products/10001007/stockItems/1?store_id=1

https://www.example.com/rest/stockitems/10001001

https://www.example.com/api/rest/stockitems/10001001

https://www.example.com/rest/stockitems/10001001

https://www.example.com/index.php/api/rest/all/V1/products/10001007

https://www.example.com/index.php/api/rest/default/V1/products/10001007/stockItems/48

等等...我尝试了大量的URL ..但它们都没有工作..

也是我的默认主题脚本之一,也会返回404错误,它使用PUT方法将优惠券代码应用于小计。

可以是我的服务器配置吗?或者是否有任何选项来启用/禁用put方法? :S我怎样才能确定PUT方法在我的网站上运作良好?或者我的PUT方法是否有可能无效

0 个答案:

没有答案