Magento2;通过 REST 更新库存 API

Magento2; update stock by REST API

我已经使用示例数据设置了本地 Magento 2.1.2 环境。我正在尝试通过 REST api (catalogInventoryStockRegistryV1 - Put) 找到更新库存的正确代码。

这是我目前得到的:

<?php

$adminUrl = 'http://www.localurl.pro/rest/V1/integration/admin/token/';
$ch = curl_init();
$data = array("username" => "admin", "password" => "66sRz97CZt7N[GdqFU");

$data_string = json_encode($data);
$ch = curl_init($adminUrl);
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);
$token=  json_decode($token);

$headers = array("Authorization: Bearer $token");

$requestUrl='http://www.localurl.pro/rest/V1/products/24-MB01/stockItems/1';
// Sample SKU

$sampleProductData = array(
        "qty" => 100
);
$productData = json_encode(array('stockItem' => $sampleProductData));
// Prints: {"stockItem":{"qty":100}}

$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, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);

此脚本出现以下错误:

string(122) "{"message":"Server cannot understand Content-Type HTTP header media type application\/x-www-form-urlencoded","trace":null}"

我不确定消息的确切含义。将不胜感激。

解决方案

Mohan Gopal 给出的解决方案。 'Content-Type: application/json' 在卷曲 header 中丢失了。

<?php

$adminUrl = 'http://www.localhost.pro/rest/V1/integration/admin/token/';
$ch = curl_init();
$data = array("username" => "admin", "password" => "66sRz97CZt7N[GdqFU");

$data_string = json_encode($data);
$ch = curl_init($adminUrl);
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);
$token=  json_decode($token);

//Use above token into header
$headers = array("Authorization: Bearer $token","Content-Type: application/json");

$skus = array(
  '24-MB04' => 10,
  '24-MB03' => 5
);

foreach ($skus as $sku => $stock) {
  $requestUrl='http://www.localurl.pro/rest/V1/products/' . $sku . '/stockItems/1';

  $sampleProductData = array(
          "qty" => $stock
  );
  $productData = json_encode(array('stockItem' => $sampleProductData));

  $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, $headers);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $response = curl_exec($ch);
  curl_close($ch);
  var_dump($response);

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

将内容类型添加到 header 并将 header 从

更改为

$headers = array("Authorization: Bearer $token");

$headers = array("Authorization: Bearer $token","Content-Type: application/json");

在第 53 行获取令牌 ID 后。