3

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?

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

7 Comments

thank you, can you tell my how should look like json object with all params that I needed? I tried this $fields = array( 'contactFilter' => (object) array( 'clicked_message.messageid' => '5', 'messages_sent.messageid' => '5' ), 'exportAttributes' => 'email' );
Nothing happens, I updated my question wth full code that i wrote now. I cant understand how to pass key and object..
your code is correct just comment line //$fields = json_encode($fields);
i get an error {"error":{"status":400,"message":"Input must be a valid JSON object","code":"bad_request"}}
{"code":"invalid_parameter","message":"Invalid export attributes format"}
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.