diff --git a/.gitignore b/.gitignore index b14f1e6..afc0cdc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ APIKEY *.pyc +*.DS_Store 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..ca63d8d --- /dev/null +++ b/php/add-subscriber-to-list.php @@ -0,0 +1,93 @@ + $_POST['emailname'], + 'status' => 'pending', + 'merge_fields' => array( + 'FNAME' => $_POST['firstname'], + 'LNAME' => $_POST['lastname'], + 'ZIPCODE' => $_POST['zipcode'] + ), + ); + + /** + * ================ + * Encode the data and setup cURL sequence + * ================ + */ + $encoded_pfb_data = json_encode($pfb_data); + $ch = curl_init(); + + /** + * ================ + * 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')); + 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); +?>