Forums

Mailchimp , cURL & CF5

afasm 26 Jul, 2016
Hey there

I'm actually trying to subscribe to a Mailchimp list with the cURL action, but I can't interface with the Mailchimp API because the Debugger tells me I do not authenticate properly.

The API documentation is saying:
curl --request POST \
--url 'https://usX.api.mailchimp.com/3.0/lists/57afe96172/members' \
--user 'anystring:apikey' \
--header 'content-type: application/json' \
--data '{"email_address":"urist.mcvankab+3@freddiesjokes.com", "status":"subscribed"}' \
--include


so I tried the CF5 curl action with the correct URL, and the user with the apikey as parameter (user= "anystring:apikey"), but the debugger tells me an http error 500

What am I doing wrong
Thanks for the help
GreyHead 26 Jul, 2016
Hi afasam,

This is really a MailChimp question - looking at their docs I think that in 'anystring:apikey' anystring needs to be replaced with your MailChimp Username and apikey with your MailChimp API Key

Bob
afasm 27 Jul, 2016
Hi Bob,

thank you for your reply!

I have entered the the right API key from Mailchimp as described in their documentation.

In the cURL Action I have entered some test-informations as followed:
Target URL: 
                 https://us13.api.mailchimp.com/3.0/lists/MYLISTID/members
Fields map:                  
                 user = "anystring:MYAPIKEY"
header = "content-type: application/json"
data = "{"email_address":"urist.mcvankab+3@freddiesjokes.com", "status":"subscribed"}"


I got the following response from the mailchim server:
Array
(
    [7] => Array
        (
            [Curl] => Array
                (
                    [0] => CURL OK : the CURL function was found on this server.
                    [1] => $curl_values: user=anystring%MYAPIKEY&header=content-type%3A+application%2Fjson&data=%22email_address%22%3A%22urist.mcvankab%2B3%40freddiesjokes.com%22%2C+%22status%22%3A%22subscribed%22
                    [2] => curl_target_url: https://us13.api.mailchimp.com/3.0/lists/MYLISTID/members
                    [3] => curl_errors: 
                    [4] => curl_info: Array
(
    [url] => https://us13.api.mailchimp.com/3.0/lists/MYLISTID/members
    [content_type] => application/problem+json; charset=utf-8
    [http_code] => 401
    [header_size] => 371
    [request_size] => 363
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.203538
    [namelookup_time] => 0.000795
    [connect_time] => 0.005355
    [pretransfer_time] => 0.034184
    [size_upload] => 203
    [size_download] => 193
    [speed_download] => 948
    [speed_upload] => 997
    [download_content_length] => 193
    [upload_content_length] => 203
    [starttransfer_time] => 0.203504
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 104.96.153.35
    [certinfo] => Array
        (
        )

)

                )

        )

)


I think there is a problem when transmitting the user credentials to the Mailchimp Server with the cURL action from CF5.

Do you know any other way how i can transmit the necessary data in a JSON Format with CF5?

I have found a good example which should work, but i dont get any output from the debugger to find the errors when i put the following code in a Custom Code action:

<?php
$data = [
    'email'     => 'johndoe@example.com',
    'status'    => 'subscribed',
    'firstname' => 'john',
    'lastname'  => 'doe'
];

    $apiKey = 'c72f8b0cfb094bb0ab442079d35015eb-us13';
    $listId = '8e9efd48d7';

     $memberId = md5(strtolower($data['email']));
    $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
    $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;

    $json = json_encode([
        'email_address' => $data['email'],
        'status'        => $data['status'], 
        'merge_fields'  => [
            'FNAME'     => $data['firstname'],
            'LNAME'     => $data['lastname']
        ]
    ]);

    $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, $json);                                                                                                                 

    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);


?>


Source: http://stackoverflow.com/a/32956160

Thank you
GreyHead 27 Jul, 2016
Hi afasam,

You can use a Custom Code action before the cURL action to json_encode the data if you need to.

You can use code like this to show debug output from your custom code in the second example
<?php
echo'<div>  $result: '.print_r(  $result, true).'</div>';
?>

Bob
This topic is locked and no more replies can be posted.