Hey there, fellow developer! Ready to supercharge your email marketing game with Campaign Monitor's API? You're in the right place. This guide will walk you through integrating Campaign Monitor's powerful API into your PHP project. We'll cover everything from setup to advanced features, so buckle up!
Before we dive in, make sure you've got:
Let's get this show on the road:
composer require campaignmonitor/createsend-php
Boom! You've got the Campaign Monitor PHP SDK. Now, let's set up a basic project structure:
campaign-monitor-integration/
├── src/
│ └── CampaignMonitorIntegration.php
├── composer.json
└── .env
Time to make friends with the API:
use CS_REST_General; $auth = array('api_key' => 'your-api-key-here'); $wrap = new CS_REST_General($auth);
Pro tip: Keep that API key safe! Use environment variables or a secure key management system.
Let's start with some bread-and-butter operations:
// Get client details $result = $wrap->get_clients(); // Create a new subscriber $result = $wrap->add_subscriber('list-id', [ 'EmailAddress' => '[email protected]', 'Name' => 'John Doe' ]); // Update subscriber info $result = $wrap->update_subscriber('list-id', '[email protected]', [ 'Name' => 'Jane Doe' ]);
Now for the fun part - creating and sending campaigns:
// Create a new campaign $campaign = array( 'Subject' => 'Check out our latest products!', 'Name' => 'May Newsletter', 'FromName' => 'Your Company', 'FromEmail' => '[email protected]', 'ReplyTo' => '[email protected]', 'HtmlUrl' => 'http://example.com/newsletter.html', 'TextUrl' => 'http://example.com/newsletter.txt', 'ListIDs' => array('list-id-1', 'list-id-2') ); $result = $wrap->create_campaign($campaign); // Send the campaign $result = $wrap->send_campaign('campaign-id'); // Get campaign stats $result = $wrap->get_campaign_summary('campaign-id');
Lists are the backbone of your email marketing. Here's how to wrangle them:
// Create a new list $result = $wrap->create_list('client-id', [ 'Title' => 'VIP Customers', 'UnsubscribePage' => 'http://example.com/unsubscribe' ]); // Add subscribers to a list $result = $wrap->add_subscribers('list-id', [ ['EmailAddress' => '[email protected]', 'Name' => 'Sub One'], ['EmailAddress' => '[email protected]', 'Name' => 'Sub Two'] ]); // Remove a subscriber from a list $result = $wrap->remove_subscriber('list-id', '[email protected]');
Stay on top of your campaign events with webhooks:
// Set up a webhook endpoint $result = $wrap->create_webhook('list-id', [ 'Events' => ['Subscribe', 'Deactivate'], 'Url' => 'http://example.com/webhook-handler', 'PayloadFormat' => 'json' ]); // Process webhook data $payload = file_get_contents('php://input'); $event = json_decode($payload, true); if ($event['Type'] === 'Subscribe') { // Handle new subscription } elseif ($event['Type'] === 'Deactivate') { // Handle unsubscribe }
Nobody's perfect, so let's handle those errors like a pro:
if (!$result->was_successful()) { $error_message = $result->response->Message; $error_code = $result->http_status_code; // Log error or throw exception }
When debugging, the var_dump($result)
is your best friend. Don't be shy, use it liberally (but not in production, okay?).
And there you have it! You're now armed and dangerous with Campaign Monitor API knowledge. Remember, this is just the tip of the iceberg. The API has tons more features to explore, so don't be afraid to dive deeper.
Happy coding, and may your open rates be ever in your favor!
For complete examples and more advanced usage, check out our GitHub repository. Pull requests welcome!