-1

I've tweaked the code and I think I'm getting closer to the solution, but still receiving an error.

public function onPluginsInitialized()
    {
         $this->add_member();
    }

public function add_member() {
    require __DIR__.'/vendor/autoload.php';

    $url = 'url';
    $api_key = 'api_key';
    $list_id = 'list_id';

    $client = new \GuzzleHttp\Client();

    $result = $client->request('POST', $url.'lists/'.$list_id.'/members', [
        'headers' => [
            'Authorization' => 'apikey '.$api_key,
            'json' => [
                'email_address' => $_POST['email'],
                'email_type'    => 'html',
                'status'        => 'subscribed'
            ]
        ],
        'verify' => false,
        'debug' => false
    ]);
}

Here's the error I'm getting:

'Undefined index: email' Where shall I define email? How to catch it from the form?

I tried to follow this example. However, my syntax is different. I shall add that the reason for my syntax is dictated by the fact that I'm trying to create a plugin for GRAV, also using Guzzle.

ekad
  • 14,436
  • 26
  • 44
  • 46
  • 1
    Better post your question here [codereview.stackexchange.com](https://codereview.stackexchange.com) – B001ᛦ Dec 04 '17 at 13:08
  • You really need to give more details about what issues you're having rather than just "what's wrong?" What are you expecting, and what's happening/not happening? Do you get any errors? Etc, etc. – Matt Fletcher Dec 04 '17 at 13:17
  • Also I really, _really_, **really** recommend not sending across unescaped `$_POST` values. You are setting yourself up for a world of hurt by doing so. – Matt Fletcher Dec 04 '17 at 13:18
  • Also 401 is "unauthorised", so there's a large chance that the issue lies in your MailChimp setup. Whitelisting of your domain, permissions set, and all that – Matt Fletcher Dec 04 '17 at 13:20
  • Thank you for reading the subject of my post before leaving your first comment. Specifically, it says: "status":401,"detail":"Your request did not include an API key.", – Oona O'Neil Dec 04 '17 at 13:24
  • Could this be of any help? https://stackoverflow.com/a/46457003/1025702 - it seems they're sending a very different kind of data structure in the request – Matt Fletcher Dec 04 '17 at 13:30
  • And fair enough, I may have missed the 401 mention on first glance-through due to the speed of going through the triage queue, but I'm only asking for more detail in order to be able to better help, there's no need to be snarky. – Matt Fletcher Dec 04 '17 at 13:37
  • @B001, please, before you recommend Code Review you should review what is on-topic there: https://codereview.stackexchange.com/help/on-topic – Stephen Rauch Dec 05 '17 at 03:50
  • @MattFletcherthanks for the link to a similar question. I'm indeed using Guzzle and trying to create a plugin for GRAV. – Oona O'Neil Dec 06 '17 at 10:35

1 Answers1

0

OK, I've finally got it sorted. I'm posting the answer in case someone else will be struggling with that one.

'headers' => [
            'Authorization' => 'apikey '.$api_key,
],
'json' => [
        'email_address' => $_POST['email'],
        'email_type'    => 'html',
        'status'        => 'subscribed'
]

Basically 'json' array should not be in the 'headers'. Also, removed 'debug' and 'verify'.