I try to use the Uniform Plugin from @mzur for the first time.
I would like to send some Form-Data to an existing Mailchimp List. Therefore I checked the Docs which explain how to use the Webhook Action, which is super helpful.
My question only concerns the âheadersâ part in the following code:
<?php
use Uniform\Form;
return function ($kirby)
{
$form = new Form([
'email_address' => [
'rules' => ['required', 'email'],
'message' => 'Please enter a valid email address',
],
]);
if ($kirby->request()->is('POST')) {
$form->webhookAction([
'url' => 'https://us6.api.mailchimp.com/3.0/lists/9e67587f52/members/',
'json' => true,
'params' => [
'method' => 'POST',
'data' => ['status' => 'pending'],
'headers' => ['Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l'],
],
]);
}
return compact('form');
};
What exactly is needed after âAuthorization: Basic âŚâ
Is this meant to hold my Mailchimp API key ?
Ok. Then there has to be something different causing the Problem.
At the Moment I exactly copied the example from the docs and replaced the url and the API Key. Now if I click Subscribe, the Mailchimp list doesnât get updated. There is also no error, which could show me what I missed.
Ive got a feeling its beacuse your not actually passing the form data through. I used a plain curl request when i did this, rather than the webhook:
$list_id = 'XXX';
$authToken = 'XXX';
// The data to send to the API
$postData = array(
"email_address" => 'somevalue',
"status" => "subscribed",
"merge_fields" => array(
"FULLNAME" => 'somevalue',
// more fields here as required
)
);
// Setup cURL
$ch = curl_init('https://us20.api.mailchimp.com/3.0/lists/'.$list_id.'/members/');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Authorization: Basic '.$authToken,
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => json_encode($postData)
));
// Send the request
$response = curl_exec($ch);
If you log into the mailchimp account, there is an error log page in there which will tell you the error that mailchimp gave when you sent the API request. That might help.
You can also shorten the above by using the remote class built into Kirby instead (which uses curl under the hood anyway.)
Hi @sigi, I am having a similar issue trying to use uniform to submit a Mailchimp subscription. Any chance you could share your controller? I have tried swapping âBasicâ to âapikeyâ, but to no availâŚ
This is my current controller which isnât working:
<?php
use Uniform\Form;
return function ($kirby)
{
$form = new Form([
'email' => [
'rules' => ['required', 'email'],
'message' => 'Please enter a valid email address',
],
]);
if ($kirby->request()->is('POST')) {
$form->webhookAction([
'url' => 'https://us5.api.mailchimp.com/3.0/lists/XXXXXXXXXX/members/',
'json' => true,
'params' => [
'method' => 'POST',
'data' => ['status' => 'pending'],
'headers' => ['Authorization: apikey XXXXXXXXXXXXXXXX-XXX'],
],
]);
if ($form->success()) {
go(page('home')->url());
}
}
return compact('form');
};
Yes, I thought about that too, but the end result I am trying to produce is an email form that includes a message field with a âSubscribe to Newsletterâ checkbox. That way when submitted the email message gets sent to the website owner, and if ticked, the email address goes through to Mailchimp.