Square API 创建项目工作代码现在返回错误

Square API Create Item working code now returning an error

在使用代码创建大约 1000 个库存项目后,Square 突然开始返回错误。

返回的错误是:

{"type":"bad_request","message":"'name' is required"}

示例代码:

$postData = array(
  'id' => 'test_1433281486',
  'name' => 'test',
  'description' => 'test',
  'variations' => array(
    'id' => 'test_v1433281486',
    'name' => 'Regular',
    'price_money' => array(
      'currency_code' => 'USD',
      'amount' => '19800',
    ),
  ),
);
$json = json_encode($postData);

$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $access_token, 'Content-Type: application/json', 'Accept: application/json'));
curl_setopt($curl, CURLOPT_URL, "https://connect.squareup.com/v1/me/items");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
$json = curl_exec($curl);
curl_close($curl);

这是 json_encoded 字符串:

{
  "id":"test_1433281486",
  "name":"test",
  "description":"test",
  "variations": {
    "id":"test_v1433281486",
    "name":"Regular",
    "price_money": {
      "currency_code":"USD",
      "amount":"19800"
    }
  }
}

我已经尝试了我所知道的一切来用代码做一个简单的创建项目,但它不再有效。始终 returns 消息 "name" 是必需的。我更新图像、费用、类别等的代码仍然可以完美运行 - 只是无法创建。

我还有 100 种新的库存物品要添加,所以让它发挥作用对业务来说势在必行。

变量需要以数组的形式传入。 JSON 应该是这样的:

{
  "id":"test_1433281486",
  "name":"test",
  "description":"test",
  "variations": [{
    "id":"test_v1433281486",
    "name":"Regular",
    "price_money": {
      "currency_code":"USD",
      "amount":"19800"
    }
  }]
}

感谢您的报告!如果变体未作为数组传入,我们将更新我们的错误消息以发送正确的消息。

这适用于所有 PHP 开发人员。这是与 Square 兼容的更新的 Create Item PHP 数组。注意嵌套的 "variations" 数组。

Square 现在 (6/15) 需要 JSON 数组中的括号 - PHP json_encode() 不会生成它们,除非您添加嵌套数组。

$postData = array(
  'id' => 'test_1433281487',
  'name' => 'test2',
  'description' => 'test2',
  'variations' => array(array(
    'id' => 'test_v1433281487',
    'name' => 'Regular',
    'price_money' => array(
      'currency_code' => 'USD',
      'amount' => '19800',
    ),
  )),
);
$json = json_encode($postData);

Here's another example of JSON brackets in PHP.

Here is the PHP json_encode() documentation.