diff --git a/php/README.md b/php/README.md new file mode 100644 index 0000000..a109059 --- /dev/null +++ b/php/README.md @@ -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. \ No newline at end of file diff --git a/php/account-details.php b/php/account-details.php new file mode 100644 index 0000000..dd348f7 --- /dev/null +++ b/php/account-details.php @@ -0,0 +1,16 @@ +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..050a931 --- /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