0

I am trying to make php code that will unsubscribe/subscribe users with given email.

I found this tutorial: http://www.sutanaryan.com/2016/10/mailchimp-api-subscribe-or-unsubscribe-user-php-script/

But I am still getting error. I think that there is a problem in this file:

require('mailchimp/Mailchimp.php');

In error log there's nothing.

Can anybody give me advice how to troubleshoot it or solve it in different way? I am beginner with almost no knowledge about PHP.

Thank you

4
  • What error are you actually getting? Commented Oct 6, 2017 at 10:58
  • Turn on error reporting for php. stackoverflow.com/questions/1053424/… Commented Oct 6, 2017 at 10:58
  • Failed to load resource: the server responded with a status of 500 () Commented Oct 6, 2017 at 11:02
  • Thank you for quick replies, guys. Commented Oct 6, 2017 at 11:03

1 Answer 1

1

I finally solved it by this simple code, no other library is needed!

<?php

function rudr_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_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);
    return $result;
}

$email = 'XXXXXXXXXXXXXXXX';
$status = 'subscribed'; // "subscribed" or "unsubscribed" or "cleaned" or "pending"
$list_id = 'XXXXXXXXXX'; // where to get it read above
$api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'; // where to get it read above
$merge_fields = array('FNAME' => 'Misha','LNAME' => 'Rudrastyh');

rudr_mailchimp_subscriber_status($email, $status, $list_id, $api_key, $merge_fields );


?>
Sign up to request clarification or add additional context in comments.

1 Comment

Hey in case it wasn't clear why your method works, the "merge_fields" key looks like it used to be "merge_vars" which caused a 500 error for me as well... I can't prove anything per say but I suspect that changed at some point in MC and they just decide to return a useless 500 error instead of something we can use. We had this integrated years ago then all of a sudden it broke. I seriously doubt some rogue programmer just went in and changed that one property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.