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

PHP example of adding a member to a list #37

Closed
wants to merge 2 commits into from

Conversation

jserrao
Copy link

@jserrao jserrao commented Apr 14, 2017

Felt like closing this big gap in your documentation. I'm sure you guys could make a more canonical example than this but it works and would help many out, judging from what I'm seeing around the net, S.O., blogs, etc.

// 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
// Any custom fields go into the weirdly named 'merge_fields'
Copy link

Choose a reason for hiding this comment

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

It's not so "weirdly" as these are the field names used for merge functionality within templates used by both the Mailchimp admin UI and Mandrill.

Search for "merge_vars":

I encourage you to remove your opinion comments from the PR. Fun to read but perhaps misleading.

Copy link
Author

Choose a reason for hiding this comment

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

I removed that language and tried to make the comments more readable. Check it out.

And I hear you from an architectural standpoint but think about dev UX here: most API users don't know what your internal architecture looks like. This took me like 45-60 minutes to figure out and I imagine it throws a lot of other people. All your API docs say for merge_field:

An individual merge var and value for a member.

*/
$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.

For what it's worth, while not a part of the PSR-2 coding standard. It's become typical to structure doc blocks with:

/**
 *
 */

References:

*/

// Set API Key and list ID to add a subscriber
$api_key = 'your-api-key-here';
Copy link

Choose a reason for hiding this comment

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

Consider using constants as they should not change for the lifetime of the script. Example:

define('API_KEY', 'your-api-key-here');

and access with:

curl_setopt($ch, CURLOPT_USERPWD, 'user:' . API_KEY);

// Any custom fields go into the weirdly named 'merge_fields'
// More here: http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#create-post_lists_list_id_members
$pfb_data = array(
'apikey' => $apiKey,
Copy link

Choose a reason for hiding this comment

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

$apiKey is an invalid variable. It should be:

   'apikey'        => $api_key,

@jserrao
Copy link
Author

jserrao commented Jul 19, 2017

Good updates @kvenk + @deezone. Updating my PR accordingly.

@jserrao jserrao closed this Jul 19, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants