I'm trying to add users to a list I've created in Mailchimp but I can't find any code examples anywhere. I've tried figuring out how to use the API but I'm very much a "Look at an example and learn" kind of person.
I've tried using version 2 of the API but nothing seems to be working despite working from examples on the net and Mailchimp says the following about earlier versions of their API on their website:
Versions 2.0 and earlier are deprecated. Only minimal support—bug fixes, security patches—will be available for those versions.
UPDATE 1: I did some further research based on TooMuchPete's answer with regards to the link on Managing Subscribers and altered some code I found here, but it won't work because the function http_build_query() doesn't deal with nested arrays. I'm not sure how to deal with the 'merge_fields' portion of adding a subscriber. My current code is below:
$postdata = http_build_query(
array(
'apikey' => $apikey,
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $name
)
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://us2.api.mailchimp.com/3.0/lists/<list_id>/members/', false, $context);
var_dump($result);
die('Mailchimp executed');
UPDATE 2: I've now resorted to using curl and I've managed to get something almost working. The data sends through to Mailchimp but I'm receiving the error "Your request did not include an API key." I'm guessing I need to authenticate as mentioned here. I've tried adding it to the http header which hasn't worked. See my code below:
$apikey = '<api_key>';
$auth = base64_encode( 'user:'.$apikey );
$data = array(
'apikey' => $apikey,
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $name
)
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://us2.api.mailchimp.com/3.0/lists/<list_id>/members/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json/r/n
Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.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_data);
$result = curl_exec($ch);
var_dump($result);
die('Mailchimp executed');
See Question&Answers more detail:
os