• Using TAGS and with PHP

Firt. Function to send the request to mailchimp and get response

function mc_request($api, $type, $target, $data = false) {
    $ch = curl_init($api['url'] . $target);
 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array
        (
        'Content-Type: application/json',
        'Authorization: ' . $api['login'] . ' ' . $api['key'],
    ));
 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'YOUR-USER-AGENT');
 
    if ($data)
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
 
    $response = curl_exec($ch);
    curl_close($ch);
 
    return $response;
}

Then we just generate the values to feed the function:

$tags = array('values','to','tag');
 
   $api = array
        (
        'login' => 'login',
        'key' => 'key from mailchimp',
        'url' => 'https://us20.api.mailchimp.com/3.0/'
    );
 
 $target = 'lists/xxxxxxxxxx/members/'; //The list we need to work with
 
//Values we are going to feed to mailchimp
 
   $fields = array(
            'email_address' => $email,
            'status' => 'subscribed',
            'merge_fields' => array('FNAME' => $name, 'LNAME' => $surname),
            'tags' =>$tags);
 
 
            //And send to function
 
        echo mc_request($api, 'POST', $target, $fields);
 
Enter your comment. Wiki syntax is allowed:
 
  • daily/mailchimp.txt
  • Last modified: 2019/04/10 11:56
  • by admin