From c0adbc6d845f8e85f9127cf7812375d57e44ec12 Mon Sep 17 00:00:00 2001 From: Clement Paris Date: Thu, 20 Aug 2015 19:31:59 +0100 Subject: [PATCH 1/2] Initial PHP example --- php/README.md | 7 +++++ php/account-details.php | 15 +++++++++ php/add-subscriber.php | 16 ++++++++++ php/lib/MailChimp.php | 70 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 php/README.md create mode 100644 php/account-details.php create mode 100644 php/add-subscriber.php create mode 100644 php/lib/MailChimp.php diff --git a/php/README.md b/php/README.md new file mode 100644 index 0000000..73ddb0c --- /dev/null +++ b/php/README.md @@ -0,0 +1,7 @@ +# 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. + +To add a subscriber to a list, replace the values of the method call in `add-subscriber.php` by your own ones. \ No newline at end of file diff --git a/php/account-details.php b/php/account-details.php new file mode 100644 index 0000000..1a68471 --- /dev/null +++ b/php/account-details.php @@ -0,0 +1,15 @@ +accountDetails(); + var_dump($response); + +?> \ No newline at end of file diff --git a/php/add-subscriber.php b/php/add-subscriber.php new file mode 100644 index 0000000..1d4f235 --- /dev/null +++ b/php/add-subscriber.php @@ -0,0 +1,16 @@ +addSubscriber(YOUR_MAILCHIMP_LIST_ID, SUBSCRIBER_FIRSTNAME, SUBSCRIBER_LASTNAME, SUBSCRIBER_EMAIL); + var_dump($response); + +?> \ No newline at end of file diff --git a/php/lib/MailChimp.php b/php/lib/MailChimp.php new file mode 100644 index 0000000..b79cf7a --- /dev/null +++ b/php/lib/MailChimp.php @@ -0,0 +1,70 @@ +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); + } + } + +?> \ No newline at end of file From d6578dd3f586da367113fe8a57313a789ade1f80 Mon Sep 17 00:00:00 2001 From: clement-paris-cko Date: Mon, 18 Jan 2016 10:03:32 +0100 Subject: [PATCH 2/2] Update README, apiKeyPath variable --- php/README.md | 3 ++- php/account-details.php | 7 ++++--- php/add-subscriber.php | 8 ++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/php/README.md b/php/README.md index 73ddb0c..a109059 100644 --- a/php/README.md +++ b/php/README.md @@ -4,4 +4,5 @@ These examples of MailChimp API v3.0 implementation in PHP have been tested with Require [cURL](http://php.net/manual/en/book.curl.php) and PHP. -To add a subscriber to a list, replace the values of the method call in `add-subscriber.php` by your own ones. \ No newline at end of file +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. \ No newline at end of file diff --git a/php/account-details.php b/php/account-details.php index 1a68471..dd348f7 100644 --- a/php/account-details.php +++ b/php/account-details.php @@ -1,10 +1,11 @@ addSubscriber(YOUR_MAILCHIMP_LIST_ID, SUBSCRIBER_FIRSTNAME, SUBSCRIBER_LASTNAME, SUBSCRIBER_EMAIL); var_dump($response);