Lasso Soft Inc. > Home

[mailchimp]

Linkmailchimp
AuthorJason Huck
CategoryEmail
Version8.5.x
LicensePublic Domain
Posted23 Feb 2009
Updated14 Nov 2011
More by this author...

Description

This custom type provides a Lasso interface to the MailChimp API. You can choose to use JSON or XML as the output format. Requires a valid API key (check here once you have an account) For JSON, [encode_json] and [decode_json] are required, and the results are returned as Lasso-native data structures (maps and arrays). For XML, [xml_tree] is required, and the results are returned as an XML Tree object. For each method listed in the API documentation (http://www.mailchimp.com/api/rtfm/), use the method name as a member tag, and submit the other parameters as standard keywords. See below for examples.

Sample Usage

// include the required tags/types
library('encode_json.inc');
library('xml_tree.inc');
library('mailchimp.inc');


// create a new mailchimp object
var('mychimp') = mailchimp( -key='YOUR_API_KEY_HERE', -output='json');


// ping the mailchimp server
// returns: "Everything's Chimpy!"
$mychimp->ping;	


// list all campaigns in your account	
// returns an array of maps containing campaign data
$mychimp->campaigns;


// get the ID of the first mailing list associated
// with your account
// returns an alphanumeric ID
$myChimp->lists->first->find('id');


// add a new subscriber to one of your mailing lists
// returns true on success, false otherwise
$myChimp->listSubscribe(
	-id='aaabbbcccddd',
	-email_address='king@kong.com',
	-merge_vars=array('FNAME'='King','LNAME'='Kong'),
	-double_optin=false
);


// get the id of the first campaign associated with
// your account - comes back as a quoted string, so
// it's necessary to strip the quotes before using
var('campaignID') = $mychimp->campaigns->first->find('id');
$campaignID->replace('"','');


// send a test email from a specific campaign
// recipients are expected to be provided as an array 
// with a zero-based index, so we have have to fudge
// it just a little
var('recipients') = array(0 = 'king@kong.com');


// returns true on success, otherwise false	
var('success') = $mychimp->campaignSendTest(
	-cid=$campaignID,
	-test_emails=$recipients
);


$success;	

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

define_type(
	'mailchimp',
	-prototype,
	-description='Lasso wrapper for the MailChimp API.'
);
	// Lasso wrapper for MailChimp API
	// author: Jason Huck/Core Five Creative
	
	// requires xml_tree for XML output, or encode_json
	// and decode_json for JSON output
	
	// defaults to JSON output and version 1.1 of the API
	// http://www.mailchimp.com/api/rtfm/
	local(
		'apikey' = string,
		'output' = 'json',
		'url' = 'http://api.mailchimp.com',
		'version' = 1.1		
	);


	// requires a valid API key to initialize
	// you can optionally set XML as the output format		
	define_tag(
		'oncreate',
		-req='key',
		-opt='output'
	);
		self->'apikey' = #key;
		local_defined('output') && (: 'json', 'xml') >> #output ? self->'output' = #output;
	/define_tag;


	// the main function which makes the call to MailChimp
	// and retrieves the results (in raw form)	
	define_tag(
		'connect',
		-req='method', -type='string',
		-req='params', -type='array', -copy,
		-encodenone
	);
		local('uri') = self->'url' + '/' + self->'version' + '/';
		#uri += '?output=' + self->'output' + '&method=' + #method;
		#params->insert('apikey' = self->'apikey');
		
		protect;
			local('out') = include_url(
				#uri,
				-postparams=#params,
				-sendmimeheaders=(: 'User-Agent' = client_browser),
				-timeout=15,
				-connecttimeout=15
			);
			
			handle_error;
				local('out') = null;
			/handle_error;
		/protect;
		
		return(#out);
	/define_tag;


	// the _unknowntag callback function lets us call any
	// API method by name without explicitly defining a
	// tag for each one	
	
	// NOTE: method names appear to be case-sensitive!	
	define_tag(
		'_unknowntag',
		-encodenone
	);
		local('params') = array;
	
		// loop through the supplied params and strip the
		// leading slash from keywords
		
		// if an array is given as the value of a param,
		// explode it for use on the query string like so:
		// ?myparam[a]=A&myparam[b]=B&myparam[c]=C
		if(params->size);
			iterate(params, local('i'));
				local('n') = string(#i->first)->removeleading('-')&;
			
				if(#i->second->isa('array'));						
					iterate(#i->second, local('j'));
						local('k') = #n + '[' + #j->first + ']';
						local('v') = #j->second;
						#params->insert(#k = #v);
					/iterate;
				else;
					#params->insert(#n = #i->second);
				/if;
			/iterate;
		/if;			
		
		// add the method name (the unknown tag) and grab
		// the response via the ->connect method
		local('response') = self->connect(tag_name, #params);
		
		// decode the response - JSON objects are converted
		// to native Lasso types, XML objects are returned
		// as XML_Tree objects, otherwise the raw response
		// is returned unaltered
		if(#response && (#response->beginswith('[') || #response->beginswith('{')) && self->'output' == 'json');
			#response = decode_json(#response);
		else(#response && #response->beginswith(''output' == 'xml');
			#response = xml_tree(#response);
		/if;
		
		return(#response);
	/define_tag;	
/define_type;

Related Tags

Comments

14 Nov 2011, Jason Huck

Added User-Agent header.

Added a User-Agent header to API calls per request of MailChimp.

14 Nov 2011, Jason Huck

Bug fix.

Corrected an issue where the JSON response would not be decoded in many cases.

10 Mar 2009, Jason Huck

Updated to use POST.

Adjusted to use POST instead of GET to avoid length limits in generated URI's.

25 Feb 2009, Jason Huck

Minor Tweak

Adjusted the decoding to avoid null->beginswith errors if the remote call to MailChimp fails.

Please log in to comment

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2014 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft