php - Square API Create Item 工作代码现在返回错误

标签 php curl square-connect

使用代码创建大约 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 个新的库存商品需要添加,因此让其发挥作用对于业务来说至关重要。

最佳答案

变体需要作为数组传入。 JSON 应该如下所示:

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

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

关于php - Square API Create Item 工作代码现在返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30631104/

相关文章:

.net - C++ 'GET' 请求或如何下载文件以在 C++ 中使用?

swift - 无法使用类型为 'perform' 的参数列表调用 '(_)'

javascript - SquareUp 支付 : Can we turn off SSL verification for development server?

php - 从 php 循环创建多个 csv 文件

json - 需要使用 Curl 上传 JSON 和图像文件

javascript - 如何动态添加表格行,包括文本框和具有不同 ID 的选择框

php - PPA : 'ppa:ondrej/php5-5.6' FORBIDDEN 403

ios - 如何从 Square Reader sdk 中删除结果弹出窗口

php - 通过表格和唯一代码重置密码

PHP 扩展安装 - phpize 错误 - 在 NGINX/PHP-FPM 上