php - 使用 PHP 将 JSON 数据发布到 API 时出现 count() 错误问题
问题描述
我在 PHP 中有一些数据,并试图发布到具有 JSON 格式数据的 API。但是当我发布数据时,我不断收到count() 错误:Parameter must be an array or an object that implement Countable at LINE 428 5.6,但错误仍然存在。
API 所需的样本数据
{"quote_id":112,
"country_residence":"Spain",
"physical_address":"*********",
"kra":"***********",
"policy_origin":"***********",
"policy_destination":"*********",
"with_spouse":1,
"spouse_id_passport":"B2345",
"spouse_name":"Spouse Name",
"spouse_dob":"1995-10-09",
"with_nok":1,
"nok_relation":"Son",
"nok_name":"NOK Full Names",
"nok_physical_address":"Physical address",
"nok_phone":"254**********",
"nok_email":"email2email.com",
"with_children":1,
"children":[
{"child_name":"abc","child_dob":"2015-05-23"}
]
}
我的 PHP 数据和 CURL
array:18 [
"quote_id" => 355
"country_residence" => "297"
"physical_address" => "MMNMNMNM"
"kra" => "A123456789P"
"policy_origin" => "297"
"policy_destination" => "375"
"with_spouse" => 1
"spouse_id_passport" => "A1234567"
"spouse_name" => "Martin"
"spouse_dob" => "1977-02-09"
"with_nok" => 1
"nok_relation" => "Son"
"nok_name" => "MNMNMN"
"nok_physical_address" => "MMNMNMNM"
"nok_phone" => "MNMNMN"
"nok_email" => "NMMNMMNMNN"
"with_children" => 1
"children" => "{"child_name":"martin","child_dob":"2018-10-30"}"
]
//Call the curl function posting the data
$res = $this->global_Curl($data, 'api/travel/save-policy-meta');
//Curl function
public function global_Curl($data, $url)
{
//dd($_ENV['API_ENDPOINT_NGINX_IP'] . '/' . $url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ('http://digital.***********' . '/' . $url));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Prevents usage of a cached version of the URL
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
$response = json_decode(curl_exec($ch));
curl_close($ch);
// Log::debug($response);
return $response;
}
解决方案
在预期的输出中......
"children":[
{"child_name":"abc","child_dob":"2015-05-23"}
]
假设您有一个子对象数组,所以它是一个数组数组。由于您只添加一个子元素,您需要将其包装到第二个数组中(注意额外的一组[]
值)......
$children = [['child_name' => $childname , 'child_dob' => $childdob]];
我认为这个想法是,如果你有几个子元素,它可以遍历它们......
$children = [['child_name' => $childname , 'child_dob' => $childdob],
['child_name' => $childname2 , 'child_dob' => $childdob2],
['child_name' => $childname3 , 'child_dob' => $childdob3]];
使用...将其添加到您的数组中
$array = array(
"quote_id" => 355,
"country_residence" => "297",
"physical_address" => "MMNMNMNM",
"kra" => "A123456789P",
"policy_origin" => "297",
"policy_destination" => "375",
"with_spouse" => 1,
"spouse_id_passport" => "A1234567",
"spouse_name" => "Martin",
"spouse_dob" => "1977-02-09",
"with_nok" => 1,
"nok_relation" => "Son",
"nok_name" => "MNMNMN",
"nok_physical_address" => "MMNMNMNM",
"nok_phone" => "MNMNMN",
"nok_email" => "NMMNMMNMNN",
"with_children" => 1,
"children" => $children,
);
header('Content-Type: application/json');
echo json_encode($array);
推荐阅读
- reactjs - SyntaxError:ScriptTransformer._transformAndBuildScript 处的意外令牌导入
- javascript - Jquery-日期转换问题
- c# - 统一生成对象
- javascript - 向浏览器输出响应
- mongodb - 使用 MongoEngineObjectType 使 ID 字段成为必需
- javascript - 从子组件更新父组件的对象数组的正确方法是什么?
- ios - iOS Swift 如何检测登录错误原因以向用户显示正确的错误警报
- node.js - 在 nodejs 中拆分和重建文件会通过某些格式更改其长度
- android - Android camera2 图像方向问题
- asp.net - 如何让命令 dotnet 恢复?