count(): Parameter must be an array or an object that implements Countable at LINE 428 error in PHP
count(): Parameter must be an array or an object that implements Countable at LINE 428 error in PHP
我正在尝试将一些数据保存到 API,但我一直收到此错误 count(): Parameter must be an array or an object that implements Countable at LINE 428, API 正在发布数据以接受对象数组,但在我的数据子变量中抛出上述错误..
请帮忙?
$children = '[{"child_name" => "Mmansa" , "child_dob" => "jdhjdhjd" }]';
$data = [
'quote_id' => $quote,
'country_residence' => $resd,
'physical_address' => $physical,
'children' => $children,
];
Post 通过 Curl
$res = $this->global_Curl($data, 'api/travel/save-policy-meta');
卷曲函数
public function global_Curl($data, $url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, (env('API_ENDPOINT_NGINX_IP') . '/' . $url));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $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
所需数据
{
"quote_id":136,
"country_residence":"Japan",
"physical_address":"Tokyo",
"children":[
{"child_name":"abc","child_dob":"23-05-2015"}
]
}
您的 API 需要 JSON 请求,您在 CURLOPT_POSTFIELDS
中将其作为数组发送,并且还有一个字段无效 JSON 字符串
此字符串无效 JSON
'[{"child_name" => "Mmansa" , "child_dob" => "jdhjdhjd" }]'
应该是这样的
'[{"child_name" : "Mmansa" , "child_dob" : "jdhjdhjd" }]'
这里不用写JSON字符串,可以用json_encode()方法将数组转成JSON字符串
改变这个
$children[] = ["child_name" => "Mmansa" , "child_dob" => "jdhjdhjd"];
$data = [
'quote_id' => $quote,
'country_residence' => $resd,
'physical_address' => $physical,
'children' => $children,
];
并试试这个代码
public function global_Curl($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'));
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,但我一直收到此错误 count(): Parameter must be an array or an object that implements Countable at LINE 428, API 正在发布数据以接受对象数组,但在我的数据子变量中抛出上述错误..
请帮忙?
$children = '[{"child_name" => "Mmansa" , "child_dob" => "jdhjdhjd" }]';
$data = [
'quote_id' => $quote,
'country_residence' => $resd,
'physical_address' => $physical,
'children' => $children,
];
Post 通过 Curl
$res = $this->global_Curl($data, 'api/travel/save-policy-meta');
卷曲函数
public function global_Curl($data, $url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, (env('API_ENDPOINT_NGINX_IP') . '/' . $url));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $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
所需数据{
"quote_id":136,
"country_residence":"Japan",
"physical_address":"Tokyo",
"children":[
{"child_name":"abc","child_dob":"23-05-2015"}
]
}
您的 API 需要 JSON 请求,您在 CURLOPT_POSTFIELDS
中将其作为数组发送,并且还有一个字段无效 JSON 字符串
此字符串无效 JSON
'[{"child_name" => "Mmansa" , "child_dob" => "jdhjdhjd" }]'
应该是这样的
'[{"child_name" : "Mmansa" , "child_dob" : "jdhjdhjd" }]'
这里不用写JSON字符串,可以用json_encode()方法将数组转成JSON字符串
改变这个
$children[] = ["child_name" => "Mmansa" , "child_dob" => "jdhjdhjd"];
$data = [
'quote_id' => $quote,
'country_residence' => $resd,
'physical_address' => $physical,
'children' => $children,
];
并试试这个代码
public function global_Curl($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'));
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;
}