Uniform by @mzur + Mailchimp API

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 ?

That should be your API token/key, yes.

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.

I have this in controller/home.php:

   <?php

    use Uniform\Form;

    return function ($site, $pages, $page) {

        $form = new Form([
            'email' => [
                'rules' => ['required', 'email'],
                'message' => 'Please enter a valid email address',
            ],
        ]);

        if (r::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 XXXXXXXXXXXXXXX'],
                ],
            ]);
        }

        return compact('form');
    };

    ?>

And this in templates/home.php:

<?php snippet('header') ?>

<form method="POST">
    <input name="EMAIL" type="email" value="<?php echo $form->old('email_address') ?>">
    <?php echo honeypot_field() ?>
    <?php echo csrf_field() ?>
    <input type="submit" value="Subscribe">
</form>

<?php snippet('footer') ?>

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.)

Not sure if it’s relevant, but use email instead of EMAIL. Capitals are not really useful here.

@Mirkokokoko not sure if the question still exists but i did the same with uniform and instead of

Authorization: Basic ...

i used

Authorization: apikey ...

and that worked for me.

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');
};

My code is not different.

But in my last projects, I created the forms with the Mailchimp Form Builder and then changed the style, as this was the easier solution for me.

Sigi

Thanks @sigi. Thanks for getting back to me.

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.