• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

Storing email and name in MailChimp from a PHP page

Hi Experts,

I'm an ASP guy, not a PHP fella but I have a PHP page that a user submits their name and email address to. At the moment it just uses PHPmailer to send these details to the client but the client now wants them stored in their MailChimp account. I have NO idea how to do this, has anyone done this, is it simple? To answer this question I require sample code, I just don't think I can do this without. Please help!

Thanks a million

/ Tobzzz
0
tobzzz
Asked:
tobzzz
  • 4
  • 2
1 Solution
 
Loganathan NatarajanLAMP DeveloperCommented:
You can find some API's available to do with MailChimp... ref. http://apidocs.mailchimp.com/
0
 
Ray PaseurCommented:
Have a look at this page: http://apidocs.mailchimp.com/api/1.3/listsubscribe.func.php

There are PHP examples at the bottom.  HTH, ~Ray
0
 
tobzzzAuthor Commented:
Thanks both. @Ray I looked at that page and the examples. It's like a foreign language to me as a basic ASP writer. I basically need the bare minimum put in and commented as example code I can customise. If this is a big task, too big for 500 points then I will scrap the question and pay a freelancer but I thought it might be fairly straightforward like:

// Add a connection to mailchimp somehow (userid and pass?)
codeHere
// insert field 1 (form data) into Name
codeHere
// insert field 2 (form data) into Email
codeHere
// Close connection
codeHere

??

Thanks once again for any input.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Ray PaseurCommented:
I think your instincts about hiring a professional to help are spot on.  Form handling in PHP is not really an elementary topic and since you are updating a data base, you will want to be sure that you're not receiving automated inputs from a 'bot or something dangerous like that.  

This shows what I would expect would be needed for some of the moving parts, however the script itself is highly data-dependent and the greatest dependencies are on external data, which is by definition, tainted.  What this sample script does not show is how you might figure out the $listId variable, or how you might go about preventing unwanted, automated access.  Those topics are the kinds of things that your professional developer would be able to help with.

Best regards, and best of luck with the project, ~Ray
<?php


// THIS IS UNTESTED CODE


/* *
 *
 * PREPARE A CALL TO THE MAIL CHIMP API
 *
 * DEPENDS ON THE FOLLOWING VARIABLES WHICH MUST BE SET BEFORE RUNNING THIS CODE
 * IN ORDER TO SET THESE VARIABLES WE WILL USE A FORM FOR THE CLIENT TO FILL IN
 *
 * $listId    IDENTIFIER OF YOUR LIST
 * $my_email  CLIENT EMAIL ADDRESS
 * $my_fname  CLIENT FIRST NAME
 * $my_lname  CLIENT LAST NAME
 *
*/ 


// MAKE SURE THAT ANY ERRORS, WARNINGS, NOTICES, AND MESSAGES ARE DISPLAYED
ini_set('display_errors', TRUE);
error_reporting(E_ALL);


// IF THE POST ARRAY IS EMPTY, PUT UP THE FORM
if (empty($_POST))
{
    // CREATE THE FORM USING HEREDOC NOTATION
    $form = <<<ENDFORM
<form method="post">
<br/>LIST ID: <input name="listId" />
<br/>YOUR EMAIL: <input name="my_email" />
<br/>YOUR FIRST NAME: <input name="my_fname" />
<br/>YOUR FAMILY NAME: <input name="my_lname" />
<br/><input type="submit" value="Subscribe now" />
</form>
ENDFORM;

    // THROW THE FORM TO THE BROWSER
    echo $form;
    die();
}


// IF WE GET HERE, THE CLIENT POSTED INFORMATION TO OUR SCRIPT
/* *
 * 
 * AT THIS POINT, IT WOULD BE A GOOD IDEA TO ADD SANITY CHECKS
 * TO VALIDATE AND FILTER THE INCOMING DATA.  THIS WILL BE NECESSARY
 * TO AVOID AUTOMATED SIGN UPS, WHICH ARE COMMON RISKS FOR SCRIPTS
 * LIKE THIS ONE.  THE PHP FUNCTION filter_var() CAN BE HELPFUL.
 *
 * YOU WILL FIND THE FORM IN THE $_POST ARRAY.
 * THE KEYS ARE THE NAME ATTRIBUTE OF THE <input> TAGS.
 * THE VALUES ARE WHATEVER THE CLIENT PUT INTO THE HTML FORM
 * SEE: http://php.net/manual/en/tutorial.forms.php
 * 
 * YOU MIGHT ALSO WANT TO ADD A "CAPTCHA" TEST TO THE FORM.
 *
 * THE CODE BELOW ASSUMES THAT THE VARIABLES HAVE BEEN EXTRACTED
 * FROM THE $_POST ARRAY, FILTERED, AND ARE ALL COMPLETELY ACCEPTABLE.
 *
*/


// LOAD THE REQUIRED CLASSES AND THE API KEY
require_once 'inc/MCAPI.class.php';
require_once 'inc/config.inc.php'; // ACCORDING TO MAILCHIMP, THIS CONTAINS THE API KEY

// INSTANTIATE THE API OBJECT USING THE MCAPI CLASS
$api = new MCAPI($apikey);

// SET THE CLIENT NAME AND GROUPING(S) VARIABLES
$merge_vars 
= array
( 'FNAME' => $my_fname
, 'LNAME' => $my_lname

// WARNING -- THIS PART OF $merge_vars IS HIGHLY SPECULATIVE
, 'GROUPINGS' => array
  ( array
    ( 'name'   => 'Your Interests:'
    , 'groups' => 'Bananas,Apples'
    )
  , array
    ( 'id'     => 22
    , 'groups' => 'Trains'
    )
  ,
  )
// END OF HIGHLY SPECULATIVE PART
)
;


// CALL THE API, SENDING THE LIST-ID, THE CLIENT EMAIL, AND THE VARIABLES
$api->listSubscribe
( $listId
, $my_email
, $merge_vars 
)
;


// TEST TO SEE IF THIS THING WORKED
if ($api->errorCode)
{
    // IF ERROR, SHOW THE MESSAGES
    echo PHP_EOL . "FAIL: listSubscribe()!";
    echo PHP_EOL . "CODE=" . $api->errorCode";
    echo PHP_EOL . "MESG=" . $api->errorMessage";
} 
else 
{
    // IF NO ERROR, SHOW SUCCESS
    echo PHP_EOL . "$my_fname $my_lname SUBSCRIBED.  LOOK FOR CONFIRMATION MESSAGE AT $my_email";
}

Open in new window

0
 
Ray PaseurCommented:
PS: In case you want to come over to the light side, this is a really good book.  With your ASP background you will find it a quick read.  Great examples!
http://www.sitepoint.com/books/phpmysql4/
0
 
tobzzzAuthor Commented:
Many thanks for this Ray, I can see much more clearly what's going on and what's required from the code, this is what I wanted.

Comments re bots and evil scripts duly noted.

Thanks again
0
 
Ray PaseurCommented:
Thanks for the points, and good luck with it! ~Ray
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now