2

在使用代码创建了大约 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"
    }
  }
}

我已经尝试了我所知道的一切,现在用代码做一个简单的创建项目,但它不再有效。总是返回消息“名称”是必需的。我更新图像、费用、类别等的代码仍然可以完美运行——只是无法创建。

我还有 100 件新的库存物品要添加,所以让它发挥作用对企业来说是必不可少的。

4

2 回答 2

1

变化需要作为数组传入。这是 JSON 的外观:

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

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

于 2015-06-03T21:56:20.150 回答
1

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

Square now (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);

这是 PHP 中 JSON 括号的另一个示例。

这是 PHP json_encode() 文档。

于 2015-06-04T00:59:39.663 回答