得到"布尔(假)"来自MailChimp API 3 POST请求

时间:2018-04-04 13:55:06

标签: php curl mailchimp

所有

我试图做一个简单的"帖子"使用3.0 API到Mailchimp;但我只是从以下代码中获得bool(false)响应。我知道MAILCHIMP_API_KEYLIST_ID变量是正确的......帮助?

我想要做的就是添加一封电子邮件&名字到特定列表。

    $auth = base64_encode( 'user:'.MAILCHIMP_API_KEY);

    $data = array(
        'apikey'        => MAILCHIMP_API_KEY,
        'email'         => $email,
        'status'        => 'subscribed',
        'merge_fields'  => array(
            'FNAME' => $name
        )
    );

    $json_data = json_encode($data);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'api.mailchimp.com/3.0/lists/'.NEW_LIST_ID.'/members/');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.$auth));
    curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);

    curl_setopt($ch, CURLOPT_POST, true);       
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
    $result = curl_exec($ch);

    var_dump($result);
    die('<br /><br />Mailchimp executed');

1 个答案:

答案 0 :(得分:0)

错过了网址中$server之后的点,也应该是email_address而不是email。但如果有人需要,下面是一个有效的例子:

    $auth = base64_encode( 'user:'.MAILCHIMP_API_KEY);

    $data = array(
        'apikey'        => MAILCHIMP_API_KEY,
        'email_address' => $email,
        'status'        => 'subscribed',
        'merge_fields'  => array(
            'FNAME' => $name
        )
    );

    $json_data = json_encode($data);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'.api.mailchimp.com/3.0/lists/'.NEW_LIST_ID.'/members');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.$auth));
    curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);

    curl_setopt($ch, CURLOPT_POST, true);       
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
    $result = curl_exec($ch);

    die('<br /><br />Mailchimp executed');