-3

I have got some data in PHP and trying to post to an API which has the data in JSON format. But when I post the data I keep getting an error of count(): Parameter must be an array or an object that implements Countable at LINE 428 ,, I am working on XAMPP version 7.2.1 and after checking online I changed the version to 5.6 but the error still persists.

Sample data required by the 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"}
  ]

}

My PHP data together with 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;
    }
Patweb
  • 133
  • 1
  • 10
  • Your `children` element does not match the API requirement. It looks like a string, not an array of objects. – Nick Nov 07 '18 at 06:22
  • @Nick Thanks,, how can I change it to match the API requirement,, am unable to figure out the bug – Patweb Nov 07 '18 at 06:24
  • You need to edit your post with the code which is generating the API data. – Nick Nov 07 '18 at 06:25
  • @Nick I have tried with the code from the API,, seems there is a problem in children array that am passing thus throwing the error.. – Patweb Nov 07 '18 at 06:37
  • To post JSON you need `curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type: application/json'));`, don't think you need the option `CURLOPT_HEADER`, if your posting data you need `curl_setopt($ch, CURLOPT_POST, 1);` – Nigel Ren Nov 07 '18 at 08:04

2 Answers2

0

Your "children" array in your main array doesn't seem like an array. Change it array format. Like,

array("child_name" => "martin", "child_dob" => "2018-10-30")

Also I don't see ',' separating the array elements. EDIT

I don't know what you mean by array:18. But if you are using count() or json encoding the parameter must be an array. Like,

$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,
    );
Abhishek
  • 372
  • 1
  • 6
  • 13
  • I have changed it but when I pass it as an array I get the following error **ErrorException Array to string conversion** seems aproblem converting to a string – Patweb Nov 07 '18 at 06:36
  • I have converted the array to an object using `json_encode($children)` but it still throws the previous error – Patweb Nov 07 '18 at 06:46
  • You have to out put the whole array. `json_encode($array);`. And you should set header as `header('Content-Type: application/json');` – Abhishek Nov 07 '18 at 06:55
  • I have done exactly that but I still get the same error – Patweb Nov 07 '18 at 07:13
0

In the expected output...

"children":[
    {"child_name":"abc","child_dob":"2015-05-23"}
  ]

It is assuming that you have an array of child objects, so it's an array of arrays. As your only adding one child element, you need to wrap this into a second array (note the extra set of [] round the value)...

 $children = [['child_name' => $childname , 'child_dob' => $childdob]];

I think the idea is that if you had several child elements it could iterate through them...

 $children = [['child_name' => $childname , 'child_dob' => $childdob],
       ['child_name' => $childname2 , 'child_dob' => $childdob2],
       ['child_name' => $childname3 , 'child_dob' => $childdob3]];

Add this into your array using...

$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);
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • I have done that but I get this error **1/1) ErrorException Array to string conversion** I resolve by conrting to a string using `json_encode($children)` and I get the error I added in the question – Patweb Nov 07 '18 at 07:16
  • You should NOT `json_encode()` `$children` , you only encode the overall array (`json_encode($array);') – Nigel Ren Nov 07 '18 at 07:18
  • When you said I should json_encode() the whole part did you mean `json_encode("children" => $children);` can`t seem to understand you. – Patweb Nov 07 '18 at 07:37
  • I've updated the answer to try and be clearer. If there is still a problem, can you check what the JSON is your getting and see what the difference is from what you need to send. – Nigel Ren Nov 07 '18 at 07:40
  • I have tested it,, am getting this JSON concerning children `"children":[{"child_name":"martin","child_dob":"2018-10-30"}]` but on the API I require this `"children":[ {"child_name":"abc","child_dob":"2015-05-23"} ]` json format,, – Patweb Nov 07 '18 at 07:50
  • When I post to the API I get this error **Array to string conversion** – Patweb Nov 07 '18 at 07:52
  • Sorry - can't see the difference. (apart from 1 more space) – Nigel Ren Nov 07 '18 at 07:52
  • Am using curl to post the data,, could be an issue of sending headers – Patweb Nov 07 '18 at 07:54
  • Things like this should be in the question! Add the code which is posting the data to the question. – Nigel Ren Nov 07 '18 at 07:56
  • Have added the curl function in the question,, please check it – Patweb Nov 07 '18 at 08:01