PHP code example of cresjie / mailchimp-api

1. Go to this page and download the library: Download cresjie/mailchimp-api 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/ */

    

cresjie / mailchimp-api example snippets






use \DrewM\MailChimp\MailChimp;

$MailChimp = new MailChimp('abc123abc123abc123abc123abc123-us1');

$result = $MailChimp->get('lists');

print_r($result);

$list_id = 'b1234346';

$result = $MailChimp->post("lists/$list_id/members", [
				'email_address' => '[email protected]',
				'status'        => 'subscribed',
			]);

print_r($result);

$list_id = 'b1234346';
$subscriber_hash = $MailChimp->subscriberHash('[email protected]');

$result = $MailChimp->patch("lists/$list_id/members/$subscriber_hash", [
				'merge_fields' => ['FNAME'=>'Davy', 'LNAME'=>'Jones'],
				'interests'    => ['2s3a384h' => true],
			]);

print_r($result);

$list_id = 'b1234346';
$subscriber_hash = $MailChimp->subscriberHash('[email protected]');

$MailChimp->delete("lists/$list_id/members/$subscriber_hash");

$list_id = 'b1234346';

$result = $MailChimp->post("lists/$list_id/members", [
				'email_address' => '[email protected]',
				'status'        => 'subscribed',
			]);

if ($MailChimp->success()) {
	print_r($result);	
} else {
	echo $MailChimp->getLastError();
}

use \DrewM\MailChimp\MailChimp;
use \DrewM\MailChimp\Batch;

$MailChimp = new MailChimp('abc123abc123abc123abc123abc123-us1');
$Batch 	   = $MailChimp->new_batch();

$Batch->post("op1", "lists/$list_id/members", [
				'email_address' => '[email protected]',
				'status'        => 'subscribed',
			]);

$Batch->post("op2", "lists/$list_id/members", [
				'email_address' => '[email protected]',
				'status'        => 'subscribed',
			]);

$Batch->post("op3", "lists/$list_id/members", [
				'email_address' => '[email protected]',
				'status'        => 'subscribed',
			]);

$result = $Batch->execute();

$MailChimp->new_batch($batch_id);
$result = $Batch->check_status();

use \DrewM\MailChimp\Webhook;

Webhook::subscribe('unsubscribe', function($data){
	print_r($data);
});

use \DrewM\MailChimp\Webhook;

$result = Webhook::receive();
print_r($result);

echo $MailChimp->getLastError();

print_r($MailChimp->getLastResponse());

print_r($MailChimp->getLastRequest());