Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Created by PhpStorm.
* User: Faisal
* Website: www.faisal-ibrahim.info
* Date: 2/12/2017
* Time: 10:07 AM
*/
$apikey = 'Your MailChimp Api Key';
$list_id = 'Your list ID';
$servername = 'localhost';
$username = 'Youre DB username';
$password = 'Your DB password';
$dbname = 'Your DB Name';
// 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';// your SQL Query goes here
$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' => 'subscribed',
'merge_fields' => array(
'FNAME' => 'eastwest',
'LNAME' => 'rehab',
)
);
$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;
}
@skylinegrid

This comment has been minimized.

Copy link

skylinegrid commented Sep 9, 2018

Hi, i have tried to implement your script in my code, but it seems that no matter what i try i always get a 500 error on batchSubscribe().
I have modified the output to straight up JSON, so as to bypass json_encode for more control on how the result comes out, and to be able to echo it. I then imploded the output, and fed it to batchSubscribe along with the API key and URL.

Any idea why i would get an error? My data has only three contacts created for testing purposes.

Here is my code:
` $sg_bulkmail_sql = new mysqli($db_host, $db_user, $db_password,$db_database);
$sg_bulkmail_query = $sg_bulkmail_sql->query("SELECT * FROM bulkmail WHERE bulkmail_vendor = 'mailchimp'");
$result = $sg_bulkmail_sql->query('SELECT * FROM accounts WHERE email_preference = 1 ORDER BY id_user ASC');
if($result){
while ($fetch = $result->fetch_object()) {
echo "


{\n
"email_address": "".$fetch->email."",\n
"status": "subscribed",\n
"merge_fields": {\n
"FNAME": "".$fetch->firstname."",\n
"LNAME": "".$fetch->lastname.""\n
}\n
},\n
";
$singleRecord[] = "
{
"email_address": "".$fetch->email."",
"status": "subscribed",
"merge_fields": {
"FNAME": "".$fetch->firstname."",
"LNAME": "".$fetch->lastname.""
}
},
";
}
}
$batchData = implode('',$singleRecord);
if($sg_bulkmail_query){
while($sg_bulkmail_fetch = $sg_bulkmail_query->fetch_object()){
$bulkmail_mailchimp_api_key = $sg_bulkmail_fetch->bulkmail_mailchimp_api_key;
$bulkmail_mailchimp_api_url = $sg_bulkmail_fetch->bulkmail_mailchimp_api_url;
$bulkmail_mailchimp_list = $sg_bulkmail_fetch->bulkmail_mailchimp_list;
$bulkmail_mailchimp_listid = $sg_bulkmail_fetch->bulkmail_mailchimp_listid;
$apikey = $bulkmail_mailchimp_api_key;
$api_url = $bulkmail_mailchimp_api_url;
$list_id = $bulkmail_mailchimp_listid;

	}
}
$finalData['operations'][] = array(
	"method"	=>	"POST",
	"path"		=>	"https://".$api_url.".api.mailchimp.com/3.0/lists/".$list_id."/members/",
	"body"		=>	$batchData
);

// print_r($finalData);
$api_response = batchSubscribe($finalData, $apikey);
$sg_bulkmail_sql->close();
`
Any help is appreciated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.