We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 43864
    • 151 Posts
    I'm using Register (login) to add a new member. That's no problem.

    I also have a checkbox to know if they want to be added to the newsletter.

    I have a posthook to run a snippet to add the mailaddress to Mailchimp.

    I get no errors, but no mailaddress is added to Mailchimp.

    I'm 100% sure the listid and the apikey are correct. The list has a double opt-in.

    I also tried without the $memberId and with curl post (instead of PUT), but nothing works.

    Here's my snippetcode. Any idea what goes wrong?

    <?php
    $checked = $hook->getValue('newsletter');
    $fname = $hook->getValue('fname');
    $lname = $hook->getValue('lname');
    $email = $hook->getValue('email');
    $listId = $hook->getValue('listid');
    $apiKey = "TheAPIkeyinMailchimp";
    $server = "us2";
    
    if ($checked == "Yes") {
        $memberId = md5(strtolower($email));
        print $url = 'https://' . $server . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;
    
        $data = array(
            'email_address'=>$email,
            'status' => 'pending',
            'merge_fields'  => [
                'FNAME'     => $fname,
                'LNAME'     => $lname            
            ]
            );
        $jsonString = json_encode($data);
    
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonString);
        $result = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
    
    }
    

    This question has been answered by sitsol. See the first response.

    • discuss.answer
      • 43864
      • 151 Posts
      I solved it! There was an obligatoir field inside the mailchimp list that causes the problem.

      So the above code is correct.