Mailchimp API 3.0 Batch Subscribe

前端 未结 1 1961
终归单人心
终归单人心 2021-01-19 05:43

Acconding to the documentation a POST to /lists/{list_id} should batch subscribe or unsubscribe list members.

If I send two members; one new member and one unsubscr

相关标签:
1条回答
  • 2021-01-19 06:21

    For Batch subscribe try this below code.This is tested and 100% working code.

    <?php
    $apikey  = ''; // Your Mailchimp ApiKey
    $list_id = ''; // your List ID Where you want to add subscriber
    
    $servername = 'localhost';
    $username   = 'root';
    $password   = '';
    $dbname     = 'dada_mail_to_mailchimp';
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die('Connection failed: ' . $conn->connect_error);
    }
    
    $sql       = 'SELECT * FROM emails Limit 2';
    $result    = $conn->query($sql);
    $finalData = [];
    if ($result->num_rows > 0) {
        // output data of each row
        while ($row = $result->fetch_assoc()) {
            $individulData = array(
                'apikey'        => $apikey,
                'email_address' => $row['email'],
                'status'        => $row['status'],//subscribe,pending,unsubscribe
                'merge_fields'  => array(
                    'FNAME' => $row['FNAME'],
                    'LNAME' => $row['LNAME'],
                )
            );
    
            $json_individulData        = json_encode($individulData);
            $finalData['operations'][] =
                array(
                    "method" => "POST",
                    "path"   => "/lists/$list_id/members/",
                    "body"   => $json_individulData
                );
        }
    }
    
    $api_response = batchSubscribe($finalData, $apikey);
    print_r($api_response);
    $conn->close();
    
    /**
     * Mailchimp API- List Batch Subscribe added function
     *
     * @param array  $data   Passed you data as an array format.
     * @param string $apikey your mailchimp api key.
     *
     * @return mixed
     */
    function batchSubscribe(array $data, $apikey)
    {
        $auth          = base64_encode('user:' . $apikey);
        $json_postData = json_encode($data);
        $ch            = curl_init();
        $dataCenter    = substr($apikey, strpos($apikey, '-') + 1);
        $curlopt_url   = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/batches/';
        curl_setopt($ch, CURLOPT_URL, $curlopt_url);
    
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
            'Authorization: Basic ' . $auth));
        curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json_postData);
    
        $result = curl_exec($ch);
        return $result;
    }
    
    0 讨论(0)
提交回复
热议问题