Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

Initial PHP example #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions php/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# MailChimp API v3.0 Examples for PHP

These examples of MailChimp API v3.0 implementation in PHP have been tested with PHP 5.5.12.

Require [cURL](http://php.net/manual/en/book.curl.php) and PHP.

You will need to replace `YOUR_MAILCHIMP_API_USER` by your own API user in `add-subscriber.php` and `account-details.php`.
Also, to add a subscriber to a list, replace the values of the `addSubscriber()` method in `add-subscriber.php` by your own ones.
16 changes: 16 additions & 0 deletions php/account-details.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

require_once('lib/MailChimp.php');
$apiKeyPath = '../APIKEY';

if (file_exists($apiKeyPath)) {
$apiKey = file_get_contents($apiKeyPath);
$MailChimp = new MailChimp(YOUR_MAILCHIMP_API_USER, $apiKey);
} else {
throw new Exception('Missing an API key in a file called "APIKEY" in the root folder', 1);
}

$response = $MailChimp->accountDetails();
var_dump($response);

?>
16 changes: 16 additions & 0 deletions php/add-subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

require_once('lib/MailChimp.php');
$apiKeyPath = '../APIKEY';

if (file_exists($apiKeyPath)) {
$apiKey = file_get_contents($apiKeyPath);
$MailChimp = new MailChimp(YOUR_MAILCHIMP_API_USER, $apiKey);
} else {
throw new Exception('Missing an API key in a file called "APIKEY" in the root folder', 1);
}

$response = $MailChimp->addSubscriber(YOUR_MAILCHIMP_LIST_ID, SUBSCRIBER_FIRSTNAME, SUBSCRIBER_LASTNAME, SUBSCRIBER_EMAIL);
var_dump($response);

?>
70 changes: 70 additions & 0 deletions php/lib/MailChimp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

class MailChimp {

function __construct( $apiUser, $apiKey ) {
$domain = explode('-', $apiKey)[1];

$this->apiUri = 'https://' . $domain . '.api.mailchimp.com/3.0';
$this->apiUser = $apiUser;
$this->auth = $apiUser . ':' . $apiKey;
}

private function execRequest( $method, $endpoint, $payload = array() ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, $this->auth);
curl_setopt($ch, CURLOPT_USERAGENT, 'apiUser');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

if ($method == 'DELETE') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
}

if ($method == 'GET' && !empty($payload)) {
$endpoint .= '?' . http_build_query($payload);
}

if ($method == 'POST') {
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
}

if ($method == 'PUT') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
}

$result = curl_exec($ch);

return json_decode($result);
}

public function addSubscriber( $listId, $firstName, $lastName, $email ) {
$endpoint = $this->apiUri . '/lists/' . $listId . '/members/';

$payload = array(
'email_address' => $email,
'status' => 'pending',
'merge_fields' => array(
'FNAME' => $firstName,
'LNAME' => $lastName
)
);

return $this->execRequest('POST', $endpoint, $payload);
}


public function accountDetails() {
$endpoint = $this->apiUri . '/';

return $this->execRequest('GET', $endpoint);
}
}

?>