0

Here is my code

$url = 'https://'.$_ENV["MAIL_CHIMP_DC"] .'.api.mailchimp.com/3.0/lists/'.$_ENV["MAIL_CHIMP_LIST_ID"].'/members';
    error_log('url-mail_chimp');
    error_log($url);
    $authorization_header=base64_encode('anystring:'.$_ENV['MAIL_CHIMP_API_KEY']);
    error_log($authorization_header);
    $ch=curl_init($url);
    $data_string = $data;
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization:Basic '.$authorization_header));
    $result =curl_exec($ch);
    error_log($result);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    error_log($httpcode);
    curl_close($ch);
    echo 'hello';

This is all inside a controller which is being called on ajax call. But when I am doing console.log(data) on jquery success handler, I am getting full response which is being sent over by curl request and also "hello" is appended to that response. I don't know how the response is being sent.

  • 1
    Try this see what you get `$result = curl_exec($ch); echo $result ; ` –  Jan 01 '20 at 20:46

1 Answers1

2

You are missing the CURLOPT_RETURNTRANSFER option to your curl call. Without this, curl_exec will not return the response of the call as a string but will output it directly. From the manual for CURLOPT_RETURNTRANSFER:

TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.

So, add

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

to your code and the output should be correctly returned into your $result variable.

Nick
  • 138,499
  • 22
  • 57
  • 95