Advertisement
thienduc0105

Batch save data to mailchimp

Apr 22nd, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.34 KB | None | 0 0
  1. //Batch list user payment to Mailchimp
  2.     $request_body_parameters = new stdClass();
  3.     $request_body_parameters->operations = array();
  4.     foreach ($sendEmail as $this_email) {
  5.         json_encode($this_email);
  6.         $email = $this_email['email'];
  7.         $first = $this_email['firstName'];
  8.         $last = $this_email['lastName'];
  9.  
  10.         $o = new stdClass();
  11.         $o->method = 'PUT';
  12.         $o->path = 'lists/' . MAILCHIMP_LIST_FESTIVAL_HK . '/members/' . md5(strtolower($email));
  13.         $o->body = json_encode(array(
  14.             'email_address' => $email,
  15.             'status' => 'subscribed',
  16.             "merge_fields" => [
  17.                 'FNAME' => $first,
  18.                 'LNAME' => $last
  19.             ]
  20.         ));
  21.         $request_body_parameters->operations[] = $o;
  22.     }
  23.     $response = wp_remote_post('https://' . substr(MAILCHIMP_APIKEY, strpos(MAILCHIMP_APIKEY, '-') + 1) . '.api.mailchimp.com/3.0/batches', array(
  24.         'method' => 'POST',
  25.         'headers' => array(
  26.             'Authorization' => 'Basic ' . base64_encode('user:' . MAILCHIMP_APIKEY)
  27.         ),
  28.         'body' => json_encode($request_body_parameters)
  29.     ));
  30.     if (wp_remote_retrieve_response_message($response) == 'OK') {
  31.         $response_body_parameters = json_decode(wp_remote_retrieve_body($response));
  32.     }
  33.  
  34. //you can remove foreach
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement