mailchimp subscription form and php doesn't work

2 replies · opened Jul 2, 2021

AAlmostnormalproductionsJul 2, 2021

Hi,
I use your subscription form php file (see below):
I entered my MailChimp API Key and MailChimp List ID in php file.
Mailchimp calls List ID - Audience ID (Audience-All Contacts-Settings-Audience Name and Defaults - there is Audience ID here)
I'm getting "Invalid Resource" message in the red box (top right corner on my website).
check:

Form:
<div class="widget-subscribe-form-result" id="contact"></div>
<form id="widget-subscribe-form" action="/include/subscribe-anp.php" method="post" class="mb-0">
<div class="input-group mx-auto">
<input type="email" id="widget-subscribe-form-email" name="widget-subscribe-form-email" class="form-control form-control-lg not-dark required email" placeholder="Enter your Email" style="border-top-left-radius: 23px; border-bottom-left-radius: 23px;">
<div class="input-group-append">
<button class="btn btn-lg bg-color" type="submit" style="border-top-right-radius: 23px; border-bottom-right-radius: 23px;">Subscribe</button>
</div>
</div>
</form>
</div>

I use your php:
<?php

$apiKey = ''; // Your MailChimp API Key
$listId = ''; // 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 . '" }';
}

}

?>

AAlmostnormalproductionsJul 2, 2021

found solution . close the case

SSemicolon WebSTAFFJul 2, 2021

Hello,

Thanks for your Patience! Glad your issues were resolved.

We have also just double checked from our end and it appears to be working fine for us. We have also just verified the Audience ID and API Keys on Our Live Previews and it is working correctly for us. You can check the Demos here: https://themes.semicolonweb.com/html/canvas/coming-soon.html .

Please do let us know if you are facing any specific errors and we will definitely check it out for you.

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