Reputation: 9
I want to update mailchimp campaign html i want to use this API https://mailchimp.com/developer/marketing/api/campaign-content/set-campaign-content/ but I am confusing how to add data in [] in $response = $client->campaigns->setContent("campaign_id", []);
Upvotes: 0
Views: 1395
Reputation: 2255
Personally, I don't use Mailchimp SDK, but I prefer to use my own function. It's very simple:
<?php
$content = 'This is a test';
// STEP #1: CREATING CAMPAIGN
$url = 'https://us2.api.mailchimp.com/3.0/campaigns';
$method = 'post';
$data = ['type' => 'regular', 'recipients' => ['list_id' => '***'], 'settings' => ['subject_line' => 'Test', 'title' => 'Test', 'from_name' => '***', 'reply_to' => '***@***', 'folder_id' => '***']];
$campaign = MC($url, $method, $data);
// STEP #2: ADDING CONTENT TO CAMPAIGN
$url = "https://us2.api.mailchimp.com/3.0/campaigns/$campaign/content";
$method = 'put';
$data = ['template' => ['id' => ***, 'sections' => ['content' => $content]]]; // Here I'm using a template: 'content' is a placeholder in my template (see MC Documentation)
MC($url, $method, $data);
function MC($url, $method, $data) {
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => ['Authorization: ***', 'Content-Type: application/json'], // *** = apiKey
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_TIMEOUT => 30
]);
$response = curl_exec($ch);
curl_close($ch);
if ($method === 'post') return json_decode($response)->id;
}
Replace *** with your data.
That's it.
It works for me. Let me know if it works for you as well.
Upvotes: 0