如何使用mailchimp API添加订阅

时间:2016-01-06 12:03:30

标签: php email mailchimp

我在 index.php 文件中有一个表单:

<form class="form-inline" action="subscribe.php">
    <input type="email" class="form-control" id="email" placeholder="Your mail" size="30">
    <button type="button" class="btn" id="emailbutton"></button>
</form>

我在网站上看到了一个答案,并将其复制到我的subscribe.php文件中,更改了API密钥和LIST ID密钥:

<?php

$apikey = '<api_key>';
        $auth = base64_encode( 'user:'.$apikey );

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

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://us2.api.mailchimp.com/3.0/lists/<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_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);                                                                                                                  

        $result = curl_exec($ch);

        var_dump($result);
        die('Mailchimp executed');
?>

但仍然没有发生任何事。我应该添加一个API文件?如果是,请给我一个链接。 谢谢你的帮助!

0 个答案:

没有答案