-2

Am trying to save some data to an API which but I keep getting this error count(): Parameter must be an array or an object that implements Countable at LINE 428,,, the API am posting data to accepts an array of objects but in my data children variable is throwing the above error..

Please assist?

$children = '[{"child_name" => "Mmansa" , "child_dob" => "jdhjdhjd" }]'; 

$data = [
      'quote_id' => $quote,
      'country_residence' => $resd,
      'physical_address' => $physical,
      'children' => $children,
];

Post via Curl

$res = $this->global_Curl($data, 'api/travel/save-policy-meta');

Curl function

 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;
    }

Data required by the API

{
"quote_id":136,
"country_residence":"Japan",
"physical_address":"Tokyo",
"children":[
    {"child_name":"abc","child_dob":"23-05-2015"}
  ]
}
Patweb
  • 133
  • 1
  • 10

1 Answers1

0

Your API need JSON request, Which you are sending as a array in CURLOPT_POSTFIELDS and also having one field as invalid JSON string

This string is not a valid JSON

'[{"child_name" => "Mmansa" , "child_dob" => "jdhjdhjd" }]'

It supposed to be like this

'[{"child_name" : "Mmansa" , "child_dob" : "jdhjdhjd" }]'

Here no need to write a JSON string, Instead of this you can use json_encode() method to convert a array to JSON string

Change this

$children[] = ["child_name" => "Mmansa" , "child_dob" => "jdhjdhjd"]; 

$data = [
  'quote_id' => $quote,
  'country_residence' => $resd,
  'physical_address' => $physical,
  'children' => $children,
];

And try this code

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;
}
sanu
  • 548
  • 7
  • 15
  • 1
    Please add some explanation of why they should *try this*. – Nigel Ren Nov 20 '18 at 07:32
  • @sanu Thanks alot,, it works fine... What if I have another children array,, can I do this: `$children[] = ["child_name" => "Mmansa" , "child_dob" => "jdhjdhjd"] ["child_name" => "Mmansa" , "child_dob" => "jdhjdhjd"]; ` – Patweb Nov 20 '18 at 07:55
  • @Patweb If you have another children array you have to add in separate line for example `$children[] = ["child_name" => "child1" , "child_dob" => "dob1"];` `$children[] = ["child_name" => "child2" , "child_dob" => "dob2"];` Wish you a happy Coding – sanu Nov 20 '18 at 07:56