Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Mailchimp Api Connector Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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+.)

  2. Basic Initialization:

    use MailchimpApiConnector\HttpAdapter\BuzzHttpAdapter;
    use MailchimpApiConnector\MailchimpApi;
    
    $adapter = new BuzzHttpAdapter();
    $mailchimp = new MailchimpApi($adapter, env('MAILCHIMP_API_KEY'));
    
  3. First Use Case: Fetch a list's activity (v2.0):

    $response = $mailchimp->call('/lists/activity', ['id' => 'YOUR_LIST_ID']);
    dd($response); // Debug the response
    

Where to Look First

  • Documentation: doc/index.md (outdated but covers core usage).
  • HTTP Adapters: Choose from BuzzHttpAdapter, CurlHttpAdapter, or GuzzleHttpAdapter (see src/HttpAdapter/).
  • API Endpoints: Refer to Mailchimp’s v2.0 API docs for valid endpoints.

Implementation Patterns

Core Workflows

  1. 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');
    
  2. Export API:

    use MailchimpApiConnector\MailchimpExportApi;
    $exportApi = new MailchimpExportApi($adapter, env('MAILCHIMP_API_KEY'));
    $response = $exportApi->call('list', ['id' => '123']); // Exports list members
    
  3. Version Switching:

    $mailchimp->setApiVersion(1.3); // Fallback to v1.x if needed
    

Integration Tips

  • 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();
    

Gotchas and Tips

Pitfalls

  1. Deprecated Package:

    • Issue: Last release in 2014; lacks OAuth2 support (Mailchimp now requires it).
    • Workaround: Use a modern alternative like spatie/laravel-mailchimp or update the package manually.
  2. HTTP Adapter Quirks:

    • BuzzHttpAdapter: Requires kriswallsmith/buzz (composer require buzz/buzz).
    • CurlHttpAdapter: May need curl PHP extension enabled.
    • GuzzleHttpAdapter: Requires guzzlehttp/guzzle.
  3. Response Handling:

    • Responses are raw arrays/strings. Parse JSON manually:
      $data = json_decode($response, true);
      
  4. API Key Format:

    • Use your_key-us12 (not just your_key). The -us12 suffix is critical for v2.0.

Debugging Tips

  • Enable Verbose Logging:
    $adapter->setDebug(true); // If supported by the adapter
    
  • Check Headers: Mailchimp v2.0 requires Accept: application/json and Content-Type: application/json for POST requests.

Extension Points

  1. Custom HTTP Adapter: Implement MailchimpApiConnector\HttpAdapter\HttpAdapterInterface to add support for other HTTP clients (e.g., Symfony’s HttpClient).

  2. Response Transformation: Extend MailchimpApi to auto-decode JSON responses:

    class ExtendedMailchimpApi extends MailchimpApi {
        protected function processResponse($response) {
            return json_decode($response, true);
        }
    }
    
  3. 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);
    }
    

Laravel-Specific Tips

  • 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);
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium