0

I have Update the particular list id with email subscription and now i want to extract all that email id and want to check if that particular email id is already exist or not and show message "email is already exist" and if not then insert that email id in mail chimp.

function mailchimp_subscriber_status( $email, $status, $list_id, $api_key, $merge_fields = array('FNAME' => '','LNAME' => '') ){
    $data = array(
        'apikey'        => $api_key,
            'email_address' => $email,
        'status'        => $status,
        'merge_fields'  => $merge_fields
    );

    $mch_api = curl_init(); // initialize cURL connection

    curl_setopt($mch_api, CURLOPT_URL, 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($data['email_address'])));
    //curl_setopt($mch_api, CURLOPT_USERPWD, 'user:' . $api_key);
    //curl_setopt($mch_api, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($mch_api, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.base64_encode( 'user:'.$api_key )));
    curl_setopt($mch_api, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
    curl_setopt($mch_api, CURLOPT_RETURNTRANSFER, true); // return the API response
    curl_setopt($mch_api, CURLOPT_CUSTOMREQUEST, 'PUT'); // method PUT
    curl_setopt($mch_api, CURLOPT_TIMEOUT, 10);
    curl_setopt($mch_api, CURLOPT_POST, true);
    curl_setopt($mch_api, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($mch_api, CURLOPT_POSTFIELDS, json_encode($data) ); // send data in json

    $result = curl_exec($mch_api);
    $httpCode=curl_getinfo($mch_api, CURLINFO_HTTP_CODE);
    //var_dump($httpCode); die;
    //$json = json_decode($result);
    //echo $json->{'status'}; die;
    //var_dump($result); die;
    return $result;
}
Ajay Paudel
  • 322
  • 1
  • 12

3 Answers3

2
<?php
require_once 'inc/MCAPI.class.php';
$api = new MCAPI('[[YOUR_API_KEY]]');
$merge_vars = array('FNAME'=>$_POST["fname"], 'LNAME'=>$_POST["lname"]);


$retval = $api->listSubscribe( '[[YOUR_LIST_ID]]', $_POST["email"], $merge_vars, 'html', false, true );

if ($api->errorCode == 214){
echo "<h4>You are already subscribed.</h4>";
} else {
echo "<h4>Thank you, you have been added to our mailing list.</h4>";
}
?>
Riddhi Rathod
  • 410
  • 2
  • 9
1
$httpCode=curl_getinfo($mch_api, CURLINFO_HTTP_CODE);  
if ($httpCode == 200) {
            $_SESSION['msg'] = '<p style="color: #34A853">You have successfully subscribed to CodexWorld.</p>';
        } else {
            switch ($httpCode) {
                case 214:
                    $msg = 'You are already subscribed.';
                    break;
                default:
                    $msg = 'Some problem occurred, please try again.';
                    break;
            }
            $_SESSION['msg'] = '<p style="color: #EA4335">'.$msg.'</p>';
        }
    }else{
        $_SESSION['msg'] = '<p style="color: #EA4335">Please enter valid email address.</p>';
    }
Riddhi Rathod
  • 410
  • 2
  • 9
0
function mailchimp_subscriber_status( $email, $status, $list_id, $api_key, $merge_fields = array('FNAME' => '','LNAME' => '') ){
    $data = array(
        'apikey'        => $api_key,
            'email_address' => $email,
        'status'        => $status,
        'merge_fields'  => $merge_fields
    );

    $mch_api = curl_init(); // initialize cURL connection

    curl_setopt($mch_api, CURLOPT_URL, 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($data['email_address'])));
    //curl_setopt($mch_api, CURLOPT_USERPWD, 'user:' . $api_key);
    //curl_setopt($mch_api, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($mch_api, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.base64_encode( 'user:'.$api_key )));
    curl_setopt($mch_api, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
    curl_setopt($mch_api, CURLOPT_RETURNTRANSFER, true); // return the API response
    curl_setopt($mch_api, CURLOPT_CUSTOMREQUEST, 'PUT'); // method PUT
    curl_setopt($mch_api, CURLOPT_TIMEOUT, 10);
    curl_setopt($mch_api, CURLOPT_POST, true);
    curl_setopt($mch_api, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($mch_api, CURLOPT_POSTFIELDS, json_encode($data) ); // send data in json

    $result = curl_exec($mch_api);
    $httpCode=curl_getinfo($mch_api, CURLINFO_HTTP_CODE);
    //var_dump($httpCode); die;
    //$json = json_decode($result);
    //echo $json->{'status'}; die;
    //var_dump($result); die;
    return $result;
}

i did this and add the subscriber but now i want to retrieve the subscriber list, i didn't use mail chimp class.

Ajay Paudel
  • 322
  • 1
  • 12