使用PHP将JSON数据发布到API时出现count()错误

使用PHP将JSON数据发布到API时出现count()错误,php,api,data-transfer,Php,Api,Data Transfer,我在PHP中获得了一些数据,并试图将其发布到一个API中,该API中包含JSON格式的数据。但是当我发布数据时,我不断得到一个错误count():参数必须是一个数组或一个在第428行实现可计数的对象,我正在使用XAMPP版本7.2.1,在线检查后,我将版本更改为5.6,但错误仍然存在 API要求的样本数据 {"quote_id":112, "country_residence":"Spain", "physical_address":"*********", "kra":"***********

我在PHP中获得了一些数据,并试图将其发布到一个API中,该API中包含JSON格式的数据。但是当我发布数据时,我不断得到一个错误count():参数必须是一个数组或一个在第428行实现可计数的对象,我正在使用XAMPP版本7.2.1,在线检查后,我将版本更改为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;
    }
主数组中的“子”数组看起来不像数组。更改它的数组格式。像

array("child_name" => "martin", "child_dob" => "2018-10-30")
此外,我没有看到“,”分隔数组元素。 编辑

我不知道你所说的数组:18是什么意思。但如果使用count()或json编码,则参数必须是数组。像

$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,
    );

在预期输出中

"children":[
    {"child_name":"abc","child_dob":"2015-05-23"}
  ]
它假设您有一个子对象数组,所以它是一个数组数组。由于您只添加了一个子元素,因此需要将其包装到第二个数组中(请注意,将该值四舍五入的一组额外的
[]

我认为这个想法是,如果你有几个子元素,它可以遍历它们

 $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);

您的
子元素
与API要求不匹配。它看起来像一个字符串,而不是一个对象数组。@Nick,谢谢,,,我如何更改它以符合API要求,,,我无法找出使用生成API数据的代码编辑帖子所需的错误。@Nick,我已尝试使用API中的代码,,似乎正在传递的子数组中存在问题,因此引发了错误..要发布JSON,您需要
curl_setopt($ch,CURLOPT_HTTPHEADERS,array('Content-Type:application/JSON')
,如果您需要发布数据,则不要认为您需要选项
CURLOPT\u HEADER
($ch,CURLOPT\u POST,1)我已经更改了它,但是当我将它作为数组传递时,我得到了以下错误ErrorException array to string conversion似乎是一个转换为Strings的问题我已经使用
json\u encode($children)
将数组转换为一个对象,但它仍然抛出了前面的错误,您必须将整个数组抛出<代码>json_编码($array)。您应该将header设置为
header('Content-Type:application/json')
我已经做了,但是我仍然得到了相同的错误我也做了,但是我得到了这个错误1/1)ErrorException数组到字符串的转换我通过使用
json\u encode($children)
转换到字符串来解决,我得到了我在问题中添加的错误你不应该
json\u encode()
$children,你只对整个数组进行编码(`json_encode($array);')当你说我应该进行json_encode()时,你的意思是
json_encode(“children”=>$children)似乎无法理解您的意思。我已更新了答案,以尽量更清楚。如果仍然有问题,你能检查一下你得到的JSON是什么,看看和你需要发送的有什么区别。我已经测试过了,我得到的这个JSON是关于孩子的
“children”:[{“child\u name”:“martin”,“child\u dob”:“2018-10-30”}]
,但是在API上我需要这个
“children”:[{“child\u name”:“abc”,“child_dob”:“2015-05-23”}]
json格式,,
$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);