3

i am newbie in mailchimp. just now i have created account and got api key. i have gone through their api for adding email address to the list but didn't help it.

i have contact us form and when user clicks on submit button i want to add user's email id in my mailchimp database.

i have tried with this code also but getting 104 error..

  $apikey = '***********-us3';
                $listID = '*******';
                $email = "********";
                $url = sprintf('https://us2.api.mailchimp.com/2.0/lists/subscribe&apikey=%s&id=%s&email_address=%s&output=json', $apikey, $listID, $email, $_SERVER['REMOTE_ADDR']);
                $ch = curl_init($url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $data = curl_exec($ch);
                $arr = json_decode($data, true);
                curl_close($ch);
        if ($arr == 1) {
            echo 'Check now your e-mail and confirm your subsciption.';
        } else {
                            echo $arr['code'];
            switch ($arr['code']) {
                case 214:
                echo 'You are already subscribed.';
                break;
                // check the MailChimp API for more options
                default:
                echo 'Unkown error...';
                break;          
            }
        }

can anyone suggest me how to achieve it?

Thanks in advance

Kalpit
  • 4,906
  • 4
  • 25
  • 43

2 Answers2

5

mailchimp provide a PHP wrapper at https://bitbucket.org/mailchimp/mailchimp-api-php which tends to make life a million times easier.

The problem what seems to be happening here is that you are doing a GET rather than a POST

try

$apikey = '**********-us3';
$listID = '******';
$email  = "**************";
$fields = array('apikey' => urlencode($apikey), 'id' => urlencode($listID), 'email_address' => urlencode($email), 'output' => 'json' );
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$url = 'https://us2.api.mailchimp.com/2.0/lists/subscribe';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$data = curl_exec($ch);
$arr = json_decode($data, true);
curl_close($ch);
if ($arr == 1) {
    echo 'Check now your e-mail and confirm your subsciption.';
} else {
                    echo $arr['code'];
    switch ($arr['code']) {
        case 214:
        echo 'You are already subscribed.';
        break;
        // check the MailChimp API for more options
        default:
        echo 'Unkown error...';
        break;          
    }
}

And let me know if that helped. If not I can see if I can try and set something up myself so I can test some code.

Stephan
  • 426
  • 5
  • 13
  • how to download above wrapper? i tried directly but couldn't. – Kalpit Mar 13 '14 at 06:28
  • https://bitbucket.org/mailchimp/mailchimp-api-php/raw/190cf58000ee4ed47900c78d5d4ef58c4cae0b49/src/Mailchimp.php – Stephan Mar 13 '14 at 11:30
  • i have downloaded and tried to implement that code but getting 500 error – Kalpit Mar 13 '14 at 11:37
  • I'll try setting it up myself and reproduce that, but that's going to have to be later today. What does your log say the error is? – Stephan Mar 13 '14 at 11:41
  • [Thu Mar 13 17:33:51 2014] [error] [client 127.0.0.1] PHP Fatal error: Call to undefined function curl_init() in /var/www/Faltu/src/Mailchimp.php on line 253 – Kalpit Mar 13 '14 at 12:06
  • Got solution. it was throwing because i haven't activated curl in my system. and now its working fine. thanks a lot – Kalpit Mar 13 '14 at 12:12
  • Hi @Stephan, does it provide the ability in JavaScript? I have a project which is about to start and will need to integrate this add-to-maillist function. Please help and thank you – Franva Mar 28 '14 at 00:20
  • @Franva No, because that would involve making public your apikey with which people can do anything they want. – Stephan Mar 28 '14 at 00:43
  • 1
    What you can do however is use the form for public subscription http://kb.mailchimp.com/article/how-can-i-add-my-signup-form-on-my-website/ and use the form endpoint and send use JS to send a request to the form endpoint it provides. – Stephan Mar 28 '14 at 00:48
  • @Stephan Hi, thx a lot for your reply. I'm going to use it in Cordova project which means users will not have access to do things other than what I make available for them in the mobile app. So, if the security issue is solved, now can I go a simple approach to use the API in JavaScript? – Franva Mar 28 '14 at 00:59
  • Hi @Stephan , can I assume that the list you mentioned and also mentioned is the article is the Mail List? – Franva Mar 28 '14 at 01:51
  • How to use merge_fields with this example? – Michael L Watson Aug 18 '17 at 11:58
1

There is another Popular Simple API https://github.com/drewm/mailchimp-api

use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp('abc123abc123abc123abc123abc123-us1');

$list_id = 'b1234346';
$result = $MailChimp->post("lists/$list_id/members", [
                'email_address' => 'davy@example.com',
                'status'        => 'subscribed',
            ]);

print_r($result);
AZinkey
  • 5,209
  • 5
  • 28
  • 46