【问题标题】:Post Json API giving error 500, what I'm doing wrong?发布 Json API 给出错误 500,我做错了什么?
【发布时间】:2021-12-17 04:15:28
【问题描述】:

我正在尝试使用 php cURL 在我的 API 中创建一个新客户端。客户、产品和所有创建的东西都是通过 POST 方法创建的。这是我的代码:

$json='{
"data": {
    "type": "customers",
    "attributes": {
        "tax_registration_number": "5555555550",
        "business_name": "Test customer",
        "contact_name": "Mr. Test",
        "website": "https://www.testurl.pt",
        "phone_number": "2299999999",
        "mobile_number": "9299999999",
        "email": "test@testcustomer.pt",
        "observations": "This is only a test",
        "internal_observations": "This is good customer",
        "not_final_customer": null,
        "cashed_vat": null,
        "tax_country_region": "PT-MA"
    },
    "relationships": {
        "main_address": {
            "data": {
                "type": "addresses",
                "id": 1
            }
        },
        "addresses": {
            "data": [
                {
                    "type": "addresses",
                    "id": 1
                },
                {
                    "type": "addresses",
                    "id": 2
                }
            ]
        }
    }
}
}';
 print($json);

这里我启动了 cURL,我已经有了令牌和授权:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,($url));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,($json));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/vnd.api+json',
    'Accept: application/json',
    'Authorization: Bearer ' . $token,
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
var_dump ($response);
$response=json_decode($response,true);
curl_close ($ch);

这是我的回复:

string(329) "{"errors":[{"status":"500 Internal Server Error","code":"JA006","detail":"Erro de sistema JA006: erro interno na base de dados. Por favor contacte o suporte técnico.","meta":{"internal-error":"in JsonapiJson::Value::operator[](ArrayIndex)const: requires arrayValue"}}],"jsonapi":{"version": "1.0","meta":{"libversion":"2.4.1"}}}"

有人可以帮我吗? 谢谢!

【问题讨论】:

  • application/vnd.api+json 看起来有点可疑。 application/json 是普通的 JSON 内容类型(就像在您的接受标头中一样)。除此之外,500 错误实际上是服务器端错误,因此您必须联系 API 的维护人员以了解其崩溃的原因。就我个人而言,我看不出您的代码有任何其他明显错误,但也许其他人会发现一些东西
  • 您从 API 收到JA006 system error: internal database error. Please contact technical support.,因此请联系他们的技术支持

标签: php json curl


【解决方案1】:

API 错误指出:in JsonapiJson::Value::operator[](ArrayIndex)const: requires arrayValue

确定 JSON 是正确的格式吗?看起来您可能在 JSON 中提供了一个 object,服务器需要一个 对象数组。例如,检查 datarelationshipsaddresses 是否应该是数组。

我的最高猜测是:

"addresses": {
  "data": [
    {
      "type": "addresses",
      "id": 1
    },
    {
      "type": "addresses",
      "id": 2
    }
  ]
}

也许应该是

"addresses":[
  {
    "type": "addresses",
    "id": 1
  },
  {
    "type": "addresses",
    "id": 2
  }
]

我当然不能肯定地告诉你,因为我不知道你尝试使用的 API,但我高度怀疑这种情况是这样的,它需要一个数组,但你提供的是一个对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 2013-11-25
    相关资源
    最近更新 更多