subscribe.php - Footer Subscribe Now - Mailchimp not working

1 reply · opened Jan 8, 2020

Ddaveh113Jan 8, 2020

I'm new to this so I'm not sure what is happening. I used the help files and edited the php to include the mailchimp API Key and List ID but when I try to use the website online I get an error message "Invalid Resource".

Does anyone have any suggestions on how to get my newsletter subscription form working with mailchimp? I noticed that mailchimp does not refer to it as List ID anymore they show it as Audience ID....would that make a difference?

<?php

$apiKey = 'I PUT MY API IN HERE'; // Your MailChimp API Key
$listId = 'I PUT MY LIST ID HERE'; // Your MailChimp List ID

if( isset( $_GET['list'] ) AND $_GET['list'] != '' ) {
$listId = $_GET['list'];
}

$email = $_POST['widget-subscribe-form-email'];
$fname = isset( $_POST['widget-subscribe-form-fname'] ) ? $_POST['widget-subscribe-form-fname'] : '';
$lname = isset( $_POST['widget-subscribe-form-lname'] ) ? $_POST['widget-subscribe-form-lname'] : '';
$datacenter = explode( '-', $apiKey );
$submit_url = "https://" . $datacenter[1] . ".api.mailchimp.com/3.0/lists/" . $listId . "/members/" ;

if( isset( $email ) AND $email != '' ) {

$merge_vars = array();
if( $fname != '' ) { $merge_vars['FNAME'] = $fname; }
if( $lname != '' ) { $merge_vars['LNAME'] = $lname; }

$data = array(
	'email_address' =&gt; $email,
	'status' =&gt; 'subscribed'
);

if( !empty( $merge_vars ) ) { $data['merge_fields'] = $merge_vars; }

$payload = json_encode($data);

$auth = base64_encode( 'user:' . $apiKey );

$header   = array();
$header[] = 'Content-type: application/json; charset=utf-8';
$header[] = 'Authorization: Basic ' . $auth;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $submit_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

$result = curl_exec($ch);
curl_close($ch);
$data = json_decode($result);

if ( isset( $data-&gt;status ) AND $data-&gt;status == 'subscribed' ){
	echo '{ "alert": "success", "message": "You have been **successfully** subscribed to our Email List." }';
} else {
	echo '{ "alert": "error", "message": "' . $data-&gt;title . '" }';
}

}

?>

SSemicolon WebSTAFFJan 10, 2020

Hello,

We have just checked this out and the Settings are still the same. Make sure that you go to Audience > Choose Current Audience > Manage Audience > Settings and then use the Unique Audience ID. We are using the same on the Demos and appears to be working fine.

As a test, consider finding the following code in the include/subscribe.php File:

if( !empty( $merge_vars ) ) { $data['merge_fields'] = $merge_vars; }

and replace it with:

$data['merge_fields'] = $merge_vars;

and then try using the Subscription Form again. Thanks for your Patience.

Meanwhile, do let us know if we can help you with anything else or if you find any further issues with Canvas.

Have the same question, or something new?

Sign in to the Canvas dashboard to reply or open your own topic. Canvas owners get direct help from the SemiColonWeb team.

Reply on the dashboard