Reputation: 95
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
Upvotes: 0
Views: 960
Reputation: 95
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 );
?>
Upvotes: 1