您的位置 首页 编程知识

使用 PHP 正确构建 REST API 的 POST 请求数据:混合数组和对象

本文将指导你如何在使用 PHP 向 REST API 发送 POST 请求时,正确构建包含混合数组和对象的数据…

使用 PHP 正确构建 REST API 的 POST 请求数据:混合数组和对象

本文将指导你如何在使用 PHP 向 REST API 发送 POST 请求时,正确构建包含混合数组和对象的数据结构。许多开发者在处理复杂的数据结构时,容易遇到数据格式不匹配的问题,导致 API 验证失败。本文将通过一个具体的例子,展示如何诊断并解决这类问题,确保你的 PHP 代码能够生成符合 API 要求的 JSON 数据。

首先,我们来看一个常见的错误配置。假设你需要发送以下 JSON 数据到 API:

{   "version": 0,   "roles": {     "customer": {     }   },   "person": {      "firstName": "Inge",      "lastName": "Musterfrau"   },   "emailAddresses" :{     "private" : [       "email@example.com"     ]   }, "addresses": {     "billing": [       {         "supplement": null,         "street": "aaa",         "zip": "12345",         "city": "Berlin",         "countryCode": "DE"       }     ]   } }
登录后复制

如果你的 PHP 代码如下:

<?php  $billingAddr = array(     "supplement" => $billingAddress["streetDetails"] ?? null, // 使用null coalescing operator     "street" => $billingAddress["street"] ?? null,     "zip" => $billingAddress["zipCode"] ?? null,     "city" => $billingAddress["city"] ?? null,     "countryCode" => $billingAddress["country"] ?? null, ); $params = [     "version"       => 0,     "roles"         => $roles ?? null, // 使用null coalescing operator     "person"        => $person ?? null, // 使用null coalescing operator     "emailAddresses"  => $emailAddresses ?? null, // 使用null coalescing operator     "addresses"     => [         "billing"  => $billingAddr     ] ];  echo json_encode($params, JSON_PRETTY_PRINT);  ?>
登录后复制

以上代码生成的 JSON 结构如下:

立即学习“”;

{     "version": 0,     "roles": null,     "person": null,     "emailAddresses": null,     "addresses": {         "billing": {             "supplement": null,             "street": null,             "zip": null,             "city": null,             "countryCode": null         }     } }
登录后复制

问题在于 addresses 中的 billing 应该是一个数组,而上述代码将其处理成了一个对象。 这会导致 API 验证失败,并抛出类似 “missing_entity – addresses – validation_flure” 的错误。

正确的做法是将 $billingAddr 放入一个数组中:

<?php  $billingAddr = array(     "supplement" => $billingAddress["streetDetails"] ?? null, // 使用null coalescing operator     "street" => $billingAddress["street"] ?? null,     "zip" => $billingAddress["zipCode"] ?? null,     "city" => $billingAddress["city"] ?? null,     "countryCode" => $billingAddress["country"] ?? null, ); $params = [     "version"       => 0,     "roles"         => $roles ?? null, // 使用null coalescing operator     "person"        => $person ?? null, // 使用null coalescing operator     "emailAddresses"  => $emailAddresses ?? null, // 使用null coalescing operator     "addresses"     => [         "billing"  => [$billingAddr]     ] ];  echo json_encode($params, JSON_PRETTY_PRINT);  ?>
登录后复制

修改后的代码生成的 JSON 结构如下:

{     "version": 0,     "roles": null,     "person": null,     "emailAddresses": null,     "addresses": {         "billing": [             {                 "supplement": null,                 "street": null,                 "zip": null,                 "city": null,                 "countryCode": null             }         ]     } }
登录后复制

现在,billing 变成了一个包含一个元素的数组,符合 API 期望的格式。

总结与注意事项:

  • 在构建复杂 JSON 数据时,务必仔细检查 API 文档,了解其期望的数据结构。
  • 使用 json_encode($data, JSON_PRETTY_PRINT) 可以方便地查看 PHP 代码生成的 JSON 结构,方便调试。
  • 在处理数组和对象混合的数据结构时,要特别注意数组的嵌套关系。
  • 使用 ?? null (null coalescing operator) 可以避免因数组键不存在而导致的错误,提高代码的健壮性。 在PHP 7 及以上版本可用。
  • 在发送 POST 请求时,确保设置正确的 Content-Type 请求头,通常为 application/json。

通过以上步骤,你应该能够正确构建包含混合数组和对象的 POST 请求数据,避免 API 验证错误,成功地将数据提交到 REST API。

以上就是使用 PHP 正确构建 REST API 的 POST 请求数据:混合数组和对象的详细内容,更多请关注php中文网其它相关文章!

本文来自网络,不代表四平甲倪网络网站制作专家立场,转载请注明出处:http://www.elephantgpt.cn/14034.html

作者: nijia

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

18844404989

在线咨询: QQ交谈

邮箱: 641522856@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部