Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

Modified PHP API Example #39

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
APIKEY
*.pyc
*.DS_Store
15 changes: 15 additions & 0 deletions php/README.md
Original file line number Diff line number Diff line change
@@ -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.
93 changes: 93 additions & 0 deletions php/add-subscriber-to-list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Add a 'member' to a 'list' via mailchimp API v3.x
* @ http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#create-post_lists_list_id_members
*
* ================
* BACKGROUND
* Typical use case is that this code would get run by an .ajax() jQuery call or possibly a form action
* The live data you need will get transferred via the global $_POST variable
* That data must be put into an array with keys that match the mailchimp endpoints, check the above link for those
* You also need to include your API key and list ID for this to work.
* You'll just have to go get those and type them in here, see README.md
* ================
*/

/**
* ================
* Sets API Key and list ID to add a subscriber
* ================
*/
define("api_key", "your-api-key-here");
define("list_id", "your-list-id-here");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually see these constants in ALL_CAPS_CASE, but I guess that's a preference thing.


/**
* ================
* DESTINATION URL
* Note: your API URL has a location subdomain at the front of the URL string
* It can vary depending on where you are in the world
* To determine yours, check the last 3 digits of your API key
* ================
*/
$url = 'https://us5.api.mailchimp.com/3.0/lists/' . list_id . '/members/';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider setting the us5 portion to a variable like list_id and api_key because it can change from user to user. For example, my list is us6


/**
* ================
* DATA SETUP
* Encode data into a format that the add subscriber mailchimp end point is looking for
* Must include 'email_address' and 'status'
* Statuses: pending = they get an email; subscribed = they don't get an email
* Custom fields go into the 'merge_fields' as another array
* More here: http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#create-post_lists_list_id_members
* ================
*/
$pfb_data = array(
'email_address' => $_POST['emailname'],
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider using $_POST['email'] because 1) I'm not sure what emailname is, and 2) it's a good practice to have your email input with the name="email" for autofilling purposes

'status' => 'pending',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider adding 'interests' => $interests, where $interests may be built like this:

  // Build the $interests array
  $interests = [];
  $interestArr = isset($_POST['interests']) && is_array($_POST['interests']) ? $_POST['interests'] : [];
  foreach ($interestArr as $id) {
    $interests[$id] = true;
  }

Copy link

@kevnk kevnk Jul 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why pending instead of subscribed? The reason I'm using this file is to subscribe users with ajax without the required double opt-in, so subscribed status is perfect for my use case.

'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);
?>