From f7f1b87cb308d02543e0e7d285362fce5e28d3e0 Mon Sep 17 00:00:00 2001 From: jserrao Date: Fri, 14 Apr 2017 17:58:57 -0400 Subject: [PATCH 1/4] PHP example of adding a member to a list --- php/README.md | 15 ++++++++ php/add-subscriber-to-list.php | 70 ++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 php/README.md create mode 100644 php/add-subscriber-to-list.php diff --git a/php/README.md b/php/README.md new file mode 100644 index 0000000..c8558d2 --- /dev/null +++ b/php/README.md @@ -0,0 +1,15 @@ +# PHP Example of using the Mailchimp API v3.x + +This is a basic example of adding a subscriber ('creating a member' in Mailchimp speak) to a list. + +## Prerequisites + +You'll need the following to do this: + +* Reference the official [Mailchimp API docs on the subject](http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#create-post_lists_list_id_members). +* [Get your API key](http://kb.mailchimp.com/integrations/api-integrations/about-api-keys) +* [Get the ID of the list you want to add people into](http://kb.mailchimp.com/integrations/api-integrations/about-api-keys) + +## Background + +Mailchimp recommends a basic HTTP authentication which we can accomplish by using PHP's cURL library. Pay particular attention to the [CURL_SETOPT](http://php.net/manual/en/function.curl-setopt.php) items, as this can prove particularly difficult to deal with. diff --git a/php/add-subscriber-to-list.php b/php/add-subscriber-to-list.php new file mode 100644 index 0000000..f01cb0e --- /dev/null +++ b/php/add-subscriber-to-list.php @@ -0,0 +1,70 @@ + $apiKey, + 'email_address' => $_POST['emailname'], + 'status' => 'pending', + 'merge_fields' => array( + 'FNAME' => $_POST['firstname'], + 'LNAME' => $_POST['lastname'], + 'ZIPCODE' => $_POST['zipcode'] + ), + ); + + // Encode the data + $encoded_pfb_data = json_encode($pfb_data); + + // Setup CURL sequence + $ch = curl_init(); + + // Options setting + // The tricky one here is the _USERPWD - this is how you transfer the API key over + // _RETURNTRANSFER allows us to get the response into a variable which is nice + // This example just POSTs, we don't edit/modify - just a simple add to a list + // _POSTFIELDS does the heavy lifting + // _SSL_VERIFYPEER should probably be set but I didn't do it here + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_TIMEOUT, 10); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_pfb_data); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + + $results = curl_exec($ch); // store response + $response = curl_getinfo($ch, CURLINFO_HTTP_CODE); // get HTTP CODE + $errors = curl_error($ch); // store errors + + curl_close($ch); + + // Returns info back to jQuery .ajax or just outputs onto the page + echo json_encode($results); +?> From bc86ef9817bb43ae079e3e94ab6aba5a3bf06316 Mon Sep 17 00:00:00 2001 From: jserrao Date: Sat, 15 Apr 2017 15:01:13 -0400 Subject: [PATCH 2/4] Updated PR per DeeZone --- php/add-subscriber-to-list.php | 48 ++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/php/add-subscriber-to-list.php b/php/add-subscriber-to-list.php index f01cb0e..354c930 100644 --- a/php/add-subscriber-to-list.php +++ b/php/add-subscriber-to-list.php @@ -1,34 +1,41 @@ $apiKey, 'email_address' => $_POST['emailname'], 'status' => 'pending', 'merge_fields' => array( @@ -41,15 +48,18 @@ // Encode the data $encoded_pfb_data = json_encode($pfb_data); - // Setup CURL sequence + // Setup cURL sequence $ch = curl_init(); - // Options setting - // The tricky one here is the _USERPWD - this is how you transfer the API key over - // _RETURNTRANSFER allows us to get the response into a variable which is nice - // This example just POSTs, we don't edit/modify - just a simple add to a list - // _POSTFIELDS does the heavy lifting - // _SSL_VERIFYPEER should probably be set but I didn't do it here + /* ================ + * cURL OPTIONS + * The tricky one here is the _USERPWD - this is how you transfer the API key over + * _RETURNTRANSFER allows us to get the response into a variable which is nice + * This example just POSTs, we don't edit/modify - just a simple add to a list + * _POSTFIELDS does the heavy lifting + * _SSL_VERIFYPEER should probably be set but I didn't do it here + * ================ + */ curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); From 06cc7d8ba51f86241181f746d499ad7ae225884a Mon Sep 17 00:00:00 2001 From: jserrao Date: Wed, 19 Jul 2017 09:56:30 -0400 Subject: [PATCH 3/4] Modified .gitignore to ignore DS_Store files on OSX --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index b14f1e6..afc0cdc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ APIKEY *.pyc +*.DS_Store From 4541ddad84777ef37ac4fd72b07fbe95c2a9573a Mon Sep 17 00:00:00 2001 From: jserrao Date: Wed, 19 Jul 2017 09:56:46 -0400 Subject: [PATCH 4/4] Addressed PR concerns --- php/add-subscriber-to-list.php | 39 ++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/php/add-subscriber-to-list.php b/php/add-subscriber-to-list.php index 354c930..ca63d8d 100644 --- a/php/add-subscriber-to-list.php +++ b/php/add-subscriber-to-list.php @@ -1,5 +1,5 @@