正在处理一种表单,通过该表单,提交给用户的数据将通过PHP curl张贴到API。从表单中,我有一个由下拉列表控制的输入,用户可以在其中填充子代详细信息。一切正常,除了我需要从API的下拉列表中发布输入内容(引发错误)。错误的根源是在从数组中发布过滤后的结果时出现问题...
〜请协助?
包含从下拉列表中输入子项的Am数组
$a= [
'child1'=> isset($request->childDetail1Dob) ? $request->childDetail1Dob : null,
'child2'=> isset($request->childDetail2Dob) ? $request->childDetail2Dob : null,
'child3'=> isset($request->childDetail3Dob) ? $request->childDetail3Dob : null,
'child4'=> isset($request->childDetail4Dob) ? $request->childDetail4Dob : null,
'child5'=> isset($request->childDetail5Dob) ? $request->childDetail5Dob : null,
];
过滤数组以消除具有空值的字段 s
$b = array_filter($a, function($k) use ($a) { return $k!=null; });
数据正在尝试发布
$data = array(
'DobPrincipalTraveller' => "1992-02-05",
'TravelStartDate' => "2018-11-23",
'TravelEndDate' => "2018-11-30",
'CoverOption' => "Grade 4",
'DobOfSpouse' => "0000-00-00",
'Children' => $b,
'WithSpouse' => $request->spouse == null ? '0' : '1'
);
通过POST将数据发布到API
$travelplan = $this->global_Curl_Meta(
$data, 'api/travel/get-plans')->data;
卷曲功能
public function global_Curl_Meta($data, $url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, (env('API_ENDPOINT_NGINX_IP') . '/' . $url));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
//Post and convert array to JSON
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
$response = json_decode(curl_exec($ch));
curl_close($ch);
return $response;
}
API所需的示例数据
{"DobPrincipalTraveller":"1978-01-22",
"TravelStartDate":"2018-11-22",
"TravelEndDate":"2018-11-25",
"CoverOption":"Standard",
"WithSpouse":"1",
"DobOfSpouse":"02-08-1979",
"Children":[
{"DateOfBirth":"2015-05-23"},
{"DateOfBirth":"2016-09-13"}
]
}
答案 0 :(得分:0)
代替$b = array_filter($a, function($k) use ($a) { return $k!=null; });
使用
$b = [];
foreach($a as $val){
if($val){
$b[]['DateOfBirth'] = $val;
}
}