Fabio Sampaio
Fabio Sampaio

Reputation: 153

Mailchimp Subscribe using API v3.0

I'm trying to subscribe an email to a MailChimp list in PHP. I've actually not a backend developer, so I'm stagnant in this :(

I'm using a MailChimp helper PHP library: https://github.com/drewm/mailchimp-api

I've already searched all the internet and all I can get is a status of 500 internal server error. I'm already in production server.

<?php

include("./inc/MailChimp.php");
use \DrewM\MailChimp\MailChimp;

$api_key = "xxxxxxxxxxx-us13";
$list_id = "7xxxxxxx4";

$MailChimp = new MailChimp($api_key);

$result = $MailChimp->post("lists/$list_id/members", [
    "email_address" => $_POST["txt_mail"],
    'merge_fields'  => ['FNAME'=>$_POST["txt_name"], 'FPHONE'=>$_POST["txt_phone"], 'FMSG'=>$_POST["txt_message"]],
    "status"        => "subscribed"
]);

if ($MailChimp->success()) {
    echo "<h4>Thank you, you have been added to our mailing list.</h4>";
} else {
    echo $MailChimp->getLastError();
} ?>

Upvotes: 3

Views: 6132

Answers (1)

scoopzilla
scoopzilla

Reputation: 883

Oh man you have NO IDEA how frustrated this problem made me when I encountered it.

Luckily, I found this handy thing by Misha Rudrastyh that works amazingly well with API 3.0. Here's the gist though:

Since I was using Wordpress I first placed the below code into my functions.php file (here it is edited with your variables)

<?php
    function rudr_mailchimp_subscriber_status( $email, $status, $list_id, $api_key, $merge_fields = array('FNAME'=> '', 'FPHONE'=> '', 'FMSG'=> '') ){
    $data = array(
         'apikey'        => $api_key,
         'email_address' => $txt_mail,
         'status'        => $status,
         'merge_fields'  => $merge_fields
    );
 $mch_api = curl_init(); // initialize cURL connection

    curl_setopt($mch_api, CURLOPT_URL, 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($data['email_address'])));
    curl_setopt($mch_api, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.base64_encode( 'user:'.$api_key )));
    curl_setopt($mch_api, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
    curl_setopt($mch_api, CURLOPT_RETURNTRANSFER, true); // return the API response
    curl_setopt($mch_api, CURLOPT_CUSTOMREQUEST, 'PUT'); // method PUT
    curl_setopt($mch_api, CURLOPT_TIMEOUT, 10);
    curl_setopt($mch_api, CURLOPT_POST, true);
    curl_setopt($mch_api, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($mch_api, CURLOPT_POSTFIELDS, json_encode($data) ); // send data in json

    $result = curl_exec($mch_api);
      return $result;
    }

THEN I added the field for variables within my form process:

<?php
    $email = $_POST['txt_mail'];
    $FNAME=$_POST['txt_name'];
    $FPHONE=$_POST['txt_phone'];
    $FMSG=$_POST['txt_message'];
    $status = 'pending'; // "subscribed" or "unsubscribed" or "cleaned" or "pending"
    $list_id = 'xxxxxxxxxxx-us13'; // where to get it read above
    $api_key = 'xxxxxxxxxxx-us13'; // where to get it read above
    $merge_fields = array('FNAME' => $FNAME, 'FPHONE' => $FPHONE, 'FMSG' => $FMSG);

    rudr_mailchimp_subscriber_status($email, $status, $list_id, $api_key, $merge_fields );              

 ?>

I hope this helps. I struggled with this for a while until I realized how to do it properly.

Upvotes: 6

Related Questions