PHP code example of mailchimp / marketing-sdk

1. Go to this page and download the library: Download mailchimp/marketing-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mailchimp / marketing-sdk example snippets




namespace Example;

use Mailchimp\MailchimpClient;
use Mailchimp\AccountExports\Requests\CreateAccountExportsRequest;
use Mailchimp\AccountExports\Types\CreateAccountExportsRequestIncludeStagesItem;

$client = new MailchimpClient(
    token: '<token>',
);
$client->accountExports->create(
    new CreateAccountExportsRequest([
        '

The SDK defaults to the `Default_` environment. To use a different environment, pass it to the client constructor:


use Mailchimp\Exceptions\MailchimpApiException;
use Mailchimp\Exceptions\MailchimpException;

try {
    $response = $client->accountExports->create(...);
} catch (MailchimpApiException $e) {
    echo 'API Exception occurred: ' . $e->getMessage() . "\n";
    echo 'Status Code: ' . $e->getCode() . "\n";
    echo 'Response Body: ' . $e->getBody() . "\n";
    // Optionally, rethrow the exception or handle accordingly.
}

use Mailchimp\MailchimpClient;

$client = new MailchimpClient(
    '<YOUR_TOKEN>',
    ['baseUrl' => 'https://api.example.com'],
);

$items = $client->accountExports->list(['limit' => 10]);

foreach ($items as $item) {
    var_dump($item);
}

foreach ($items->getPages() as $page) {
    foreach ($page->getItems() as $pageItem) {
        var_dump($pageItem);
    }
}

use Mailchimp\MailchimpClient;

// Pass any PSR-18 compatible HTTP client implementation.
// For example, using Guzzle:
$customClient = new \GuzzleHttp\Client([
    'timeout' => 5.0,
]);

$client = new MailchimpClient(options: [
    'client' => $customClient
]);

// Or using Symfony HttpClient:
// $customClient = (new \Symfony\Component\HttpClient\Psr18Client())
//     ->withOptions(['timeout' => 5.0]);
//
// $client = new MailchimpClient(options: [
//     'client' => $customClient
// ]);

$response = $client->accountExports->create(
    ...,
    options: [
        'maxRetries' => 0 // Override maxRetries at the request level
    ]
);

$response = $client->accountExports->create(
    ...,
    options: [
        'timeout' => 3.0 // Override timeout at the request level
    ]
);