【问题标题】:Square API Create Item working code now returning an errorSquare API Create Item 工作代码现在返回错误
【发布时间】:2015-08-18 07:30:38
【问题描述】:

在使用代码创建了大约 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 件新的库存物品要添加,所以让它发挥作用对企业来说是必不可少的。

【问题讨论】:

    标签: php curl square-connect


    【解决方案1】:

    变量需要作为数组传入。以下是 JSON 的外观:

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

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

    【讨论】:

    【解决方案2】:

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

    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);
    

    Here's another example of JSON brackets in PHP.

    Here is the PHP json_encode() documentation.

    【讨论】:

      猜你喜欢
      • 2017-01-18
      • 2020-07-19
      • 2018-10-22
      • 2013-02-19
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2013-08-30
      • 1970-01-01
      相关资源
      最近更新 更多