dcsg/mailchimp-api-connector
Lightweight PHP 5.3+ connector for Mailchimp API (v1.x/v2.0) and Export API (v1.0). Provides an abstract, simple interface to call endpoints with pluggable HTTP adapters. Not an API wrapper; focuses on connecting and transporting requests.
Install via Composer:
composer require dcsg/mailchimp-api-connector:2.0.*
(Note: The package is outdated; prefer modern alternatives like spatie/laravel-mailchimp for Laravel 8+.)
Basic Initialization:
use MailchimpApiConnector\HttpAdapter\BuzzHttpAdapter;
use MailchimpApiConnector\MailchimpApi;
$adapter = new BuzzHttpAdapter();
$mailchimp = new MailchimpApi($adapter, env('MAILCHIMP_API_KEY'));
First Use Case: Fetch a list's activity (v2.0):
$response = $mailchimp->call('/lists/activity', ['id' => 'YOUR_LIST_ID']);
dd($response); // Debug the response
doc/index.md (outdated but covers core usage).BuzzHttpAdapter, CurlHttpAdapter, or GuzzleHttpAdapter (see src/HttpAdapter/).API Calls:
// GET request (default)
$response = $mailchimp->call('/lists', ['id' => '123']);
// POST request (e.g., for creating resources)
$response = $mailchimp->call('/lists', ['id' => '123'], 'POST');
Export API:
use MailchimpApiConnector\MailchimpExportApi;
$exportApi = new MailchimpExportApi($adapter, env('MAILCHIMP_API_KEY'));
$response = $exportApi->call('list', ['id' => '123']); // Exports list members
Version Switching:
$mailchimp->setApiVersion(1.3); // Fallback to v1.x if needed
Laravel Service Provider:
Bind the connector as a singleton in AppServiceProvider:
$this->app->singleton('mailchimp', function ($app) {
$adapter = new BuzzHttpAdapter();
return new MailchimpApi($adapter, env('MAILCHIMP_API_KEY'));
});
Error Handling:
Wrap calls in a try-catch to handle HTTP errors:
try {
$response = $mailchimp->call('/lists/123');
} catch (\Exception $e) {
Log::error("Mailchimp API Error: " . $e->getMessage());
}
Configuration:
Store API keys in .env:
MAILCHIMP_API_KEY=your_key-us12
Testing:
Use CurlHttpAdapter for testing (no external dependencies):
$adapter = new CurlHttpAdapter();
Deprecated Package:
spatie/laravel-mailchimp or update the package manually.HTTP Adapter Quirks:
kriswallsmith/buzz (composer require buzz/buzz).curl PHP extension enabled.guzzlehttp/guzzle.Response Handling:
$data = json_decode($response, true);
API Key Format:
your_key-us12 (not just your_key). The -us12 suffix is critical for v2.0.$adapter->setDebug(true); // If supported by the adapter
Accept: application/json and Content-Type: application/json for POST requests.Custom HTTP Adapter:
Implement MailchimpApiConnector\HttpAdapter\HttpAdapterInterface to add support for other HTTP clients (e.g., Symfony’s HttpClient).
Response Transformation:
Extend MailchimpApi to auto-decode JSON responses:
class ExtendedMailchimpApi extends MailchimpApi {
protected function processResponse($response) {
return json_decode($response, true);
}
}
Rate Limiting:
Add retry logic for 429 Too Many Requests errors:
if ($response->getStatusCode() === 429) {
sleep(10); // Wait 10 seconds before retrying
return $this->call($endpoint, $params, $method);
}
Queue Mailchimp Tasks: Use Laravel Queues to avoid timeouts for large exports:
MailchimpExportApi::dispatch($adapter, env('MAILCHIMP_API_KEY'), 'list', ['id' => '123']);
(Note: Requires custom job class.)
Cache API Responses: Cache frequent calls (e.g., list metadata) using Laravel’s cache:
$cacheKey = "mailchimp_list_{$listId}";
return Cache::remember($cacheKey, 3600, function () use ($mailchimp, $listId) {
return $mailchimp->call('/lists/' . $listId);
});
How can I help you explore Laravel packages today?