From 131dfbcf022e9a32baa0fb5bbd857cdc53756cb7 Mon Sep 17 00:00:00 2001 From: Sorin Date: Wed, 20 Jan 2016 21:38:12 +0200 Subject: [PATCH 1/3] Create README.md --- php/README.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 php/README.md diff --git a/php/README.md b/php/README.md new file mode 100644 index 0000000..ca780e5 --- /dev/null +++ b/php/README.md @@ -0,0 +1,5 @@ +# MailChimp API v3.0 Examples for PHP + +These examples have been tested with PHP version 5.6 and requires the cURL library. + +Place your API Key in the example.php file. From 2dfeb860a7ac37400bed1c1077be3ca3feddb904 Mon Sep 17 00:00:00 2001 From: Sorin Date: Wed, 20 Jan 2016 21:39:30 +0200 Subject: [PATCH 2/3] Create mailchimp.php --- php/mailchimp.php | 132 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 php/mailchimp.php diff --git a/php/mailchimp.php b/php/mailchimp.php new file mode 100644 index 0000000..540db7b --- /dev/null +++ b/php/mailchimp.php @@ -0,0 +1,132 @@ +api_uri = 'https://' . $domain . '.api.mailchimp.com/3.0'; + $this->api_key = $api_key; + } + + private function _execute_request($method, $endpoint, Array $payload = []) + { + $endpoint = $this->api_uri.$endpoint; + + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); + curl_setopt($ch, CURLOPT_USERPWD, $this->api_user.':'.$this->api_key); + curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($ch, CURLOPT_TIMEOUT, 10); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); + + switch ($method) + { + case 'DELETE': + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); + break; + case 'GET': + $endpoint .= '?' . http_build_query($payload); + break; + case 'DELETE': + $endpoint .= '?' . http_build_query($payload); + break; + case 'POST': + curl_setopt($ch, CURLOPT_POST, TRUE); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); + break; + case 'PUT': + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); + break; + } + + curl_setopt($ch, CURLOPT_URL, $endpoint); + $result = curl_exec($ch); + + return json_decode($result); + } + + /** + * List: Add a new subscriber + * @docs http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/ + * @param array $options + * @return mixed + * @throws MailChimpException + * + * Usage Example: + * $obj->list_add_subscriber([ + * 'id_list' => 'ID_LIST', // Required + * 'email' => 'EMAIL', // Required + * 'status' => 'STATUS', // Optional + * 'merge_fields' => [ // Optional + * 'FNAME' => 'NAME', + * ... + * ], + * ]); + * + */ + public function list_add_subscriber(Array $options) + { + // Check required params + if ( ! isset($options['id_list']) OR ! isset($options['email'])) + { + throw new MailChimpException('Parameters not set on '.__METHOD__); + } + + $endpoint = '/lists/' . $options['id_list'] . '/members/'; + $payload = [ + 'email_address' => $options['email'], + 'status' => isset($options['status']) ? $options['status'] : 'pending', + ]; + if (isset($options['merge_fields']) AND $options['merge_fields']) + { + $payload['merge_fields'] = $options['merge_fields']; + } + + return $this->_execute_request('POST', $endpoint, $payload); + } + + + /** + * Campaign: Send a campaign + * @docs http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#action-post_campaigns_campaign_id_actions_send + * @param array $options + * @return mixed + * @throws MailChimpException + * + * Usage Example: + * $obj->campaign_send([ + * 'id_campaign' => 'ID_CAMPAIGN', // Required + * ]); + * + */ + public function campaign_send(Array $options) + { + // Check required params + if ( ! isset($options['id_campaign'])) + { + throw new MailChimpException('Parameters not set on '.__METHOD__); + } + + $endpoint = '/campaigns/' . $options['id_campaign'] . '/actions/send/'; + + return $this->_execute_request('POST', $endpoint); + } +} + +?> From 138aadc95e30e9737774e5d0d8c5f076d5b5c553 Mon Sep 17 00:00:00 2001 From: Sorin Date: Wed, 20 Jan 2016 21:44:22 +0200 Subject: [PATCH 3/3] Create example.php --- php/example.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 php/example.php diff --git a/php/example.php b/php/example.php new file mode 100644 index 0000000..f95a899 --- /dev/null +++ b/php/example.php @@ -0,0 +1,18 @@ +list_add_subscriber([ + 'id_list' => 'ID_LIST', + 'email' => 'EMAIL', + 'status' => 'STATUS', + 'merge_fields' => [ + 'FNAME' => 'FIRST NAME', + ], +]); + +echo '
';
+print_r($reply);
+echo '
';