pacely/mailchimp-apiv3
PHP wrapper for Mailchimp API v3 with Laravel 5 support (service provider, facade, config publishing). Provides a simple request/get/post/put interface plus helpers for pagination, filtering, partial responses, and proxy setups, with practical examples.
Installation:
composer require pacely/mailchimp-apiv3:dev-master
(Note: Use dev-master as the last release is outdated. Consider forking or checking for updates.)
Register Service Provider (in config/app.php):
'providers' => [
// ...
Mailchimp\MailchimpServiceProvider::class,
],
Publish Config (optional but recommended):
php artisan vendor:publish --provider="Mailchimp\MailchimpServiceProvider"
Configure .env with:
MAILCHIMP_API_KEY=your_api_key_here
MAILCHIMP_DATA_CENTER=your_data_center
First Use Case: Fetch all lists via facade:
$lists = MC::lists()->get();
CRUD Operations:
// Create a list
$list = MC::lists()->create(['name' => 'Newsletter']);
// Fetch a list
$list = MC::lists()->get($list->id);
// Update a list
$list->update(['name' => 'Updated Newsletter']);
// Delete a list
$list->delete();
Subresources: Access nested resources (e.g., list members):
$members = MC::lists()->members($listId)->get();
$member = MC::lists()->members($listId)->create(['email' => 'user@example.com']);
Pagination:
Use ->get() with ?per_page=100 or handle via Collection:
$members = MC::lists()->members($listId)->get(['per_page' => 50]);
while ($members->nextPageExists()) {
$members = $members->nextPage();
}
Filtering:
Pass query params to get():
$members = MC::lists()->members($listId)->get([
'status' => 'subscribed',
'fields' => 'email,status'
]);
Service Container Binding: Bind custom configurations in a service provider:
$this->app->singleton('mailchimp', function ($app) {
return new Mailchimp\Mailchimp([
'api_key' => $app['config']['mailchimp.api_key'],
'data_center' => $app['config']['mailchimp.data_center'],
'proxy' => $app['config']['mailchimp.proxy'] ?? null,
]);
});
Events & Observers: Trigger actions post-Mailchimp API calls (e.g., sync local DB):
MC::lists()->create($data)->then(function ($list) {
event(new ListCreated($list));
});
Queued Jobs: Offload heavy operations (e.g., bulk updates):
dispatch(new SyncMailchimpListsJob());
Deprecated Package:
guzzlehttp/guzzle to ^7.0).API Key Format:
MAILCHIMP_API_KEY is prefixed with - (e.g., -us123abc123). Example:
MAILCHIMP_API_KEY=-us123abc123
Proxy Issues:
.env:
MAILCHIMP_PROXY=http://proxy.example.com:8080
new Mailchimp\Mailchimp(['proxy' => 'http://proxy:port']);
Rate Limiting:
429 errors gracefully:
try {
$response = MC::lists()->get();
} catch (\GuzzleHttp\Exception\RequestException $e) {
if ($e->getResponse()->getStatusCode() === 429) {
sleep(1); // Retry after delay
retry();
}
}
Enable Guzzle Debugging:
Add to config/mailchimp.php:
'debug' => env('MAILCHIMP_DEBUG', false),
Logs requests/responses to storage/logs/mailchimp.log.
Common Errors:
Invalid API Key: Verify MAILCHIMP_API_KEY and data center.Resource Not Found: Check IDs (e.g., list/member IDs are strings, not integers).Invalid Status: Use exact Mailchimp status values (e.g., 'subscribed', not 'active').Custom Endpoints: Extend the base client for unsupported endpoints:
class CustomMailchimp extends Mailchimp\Mailchimp {
public function customEndpoint($endpoint, $method = 'GET', $data = []) {
return $this->request($endpoint, $method, $data);
}
}
Response Wrapping: Decorate responses for consistency:
MC::lists()->get()->map(function ($item) {
return (object) array_merge($item->toArray(), ['custom_field' => 'value']);
});
Testing: Mock the client in tests:
$mock = Mockery::mock('overload:Mailchimp\Mailchimp');
$mock->shouldReceive('request')->andReturn(['data' => 'test']);
How can I help you explore Laravel packages today?