如何用curl发送数组和对象

I am a beginner with curl. I want to make a request where I post different data.

I have a code

$curl = curl_init();


$fields = (object) array(
     'contactFilter' => (object) array(
          'clicked_message.messageid' => '5',
          'messages_sent.messageid' => '5'
      ),
     'exportAttributes' => 'email',
   );

$fields = json_encode($fields);

$fields_string = http_build_query($fields);

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.sendinblue.com/v3/contacts/export",
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
  'Content-Type: application/json',
  'api-key: my-key-12345',
  ),
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $fields_string
));

$response = curl_exec($curl);

Documentation says that I need;

contactFilter object (X= campaign id): {"clicked_message.messageid": X,"messages_sent.messageid": X}

and

exportAttributes` array of strings For example, `['fname', 'lname, 'email'].

how should my request should look like?

You need to use CURLOPT_POSTFIELDS to post the fields, for example

$fields = array(
        'username' => "annonymous",
        'api_key' =>  "1234"
    ); 
 $fields_string = http_build_query($fields);

Now use variable fields_string to send array data

   curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);

Your curl request will be look like

curl_setopt_array($curl,
  array(
    CURLOPT_URL => "https://api.sendinblue.com/v3/contacts/export",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $fields_string
  )
);

Updated Code-

$curl = curl_init();


$fields = (object) array(
 'contactFilter' => (object) array(
      'clicked_message.messageid' => '5',
      'messages_sent.messageid' => '5'
  ),
 'exportAttributes' => 'email',
);

$fields = json_encode($fields);

//$fields_string = http_build_query($fields);

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.sendinblue.com/v3/contacts/export",
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
   'Content-Type: application/json',
   'api-key: my-key-12345',
  ),
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $fields
));

$response = curl_exec($curl);
echo $response;

print_r($response);