mailchimp api 2.0通过php订阅?

时间:2013-08-25 09:12:09

标签: php mailchimp

我需要一个如何订阅电子邮件地址到mailchimp简报的示例。

请在此处查看新的api链接: https://bitbucket.org/mailchimp/mailchimp-api-php

这是新的applehimp api,我不知道如何使用它。 :(

对于MailChimp 2.0 API,不适用于1.3。

请有人提供一个关于如何订阅用户mailchimp的示例。

谢谢。

Edit1:已经尝试过以下代码,但无法正常工作:

$merge_vars = array('MM1'=>$mm1);
$MailChimp = new Mailchimp($apikey);
$result = $MailChimp->call('lists/subscribe', array(
            'id'                => $listid,
            'email'             => array('email'=>$email),
            'merge_vars'        => $merge_vars,
            'double_optin'      => false,
            'update_existing'   => true,
            'replace_interests' => false,
            'send_welcome'      => false,
        ));
    print_r($result);

但不行。抛出以下错误: 致命错误:在第22行的subscribe.php中调用非对象的成员函数call()

5 个答案:

答案 0 :(得分:14)

参考文档,这应该是这样的:

$merge_vars = array('MM1'=>$mm1);
$listid = 'YOURLISTID';
$MailChimp = new Mailchimp($apikey);
$result = $MailChimp->lists->subscribe($listid,
                                        array('email'=>"contact@twittstrap.com"),
                                        $merge_vars,
                                        false,
                                        true,
                                        false,
                                        false
                                       );
    print_r($result);

经过测试和工作。

答案 1 :(得分:9)

这可能对一些简单的mailchimp订阅者API代码示例有用,

mailchimp subscriber API code example in PHP

答案 2 :(得分:4)

以下是Try& Catch(重复电子邮件时的示例)

header('Content-Type: application/json');
include_once 'Mailchimp.php';

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


$email = 'hello@email.com';
$merge_vars = array();

$Mailchimp = new Mailchimp($api_key);
$Mailchimp_Lists = new Mailchimp_Lists($Mailchimp);

try{
    $subscriber = $Mailchimp_Lists->subscribe(
        $list_id,
        array('email'=>htmlentities($email)),
        $merge_vars,
        false,
        false,
        false,
        false
    );
    echo json_encode(array('status' => !empty($subscriber['leid'])?'submitted':'error'));
} catch(Mailchimp_Error $e){
    echo json_encode(array(
            'status' => 'error',
            'message' => $e->getMessage()
        ));
}

了解subscribe()https://apidocs.mailchimp.com/api/2.0/lists/subscribe.php

答案 3 :(得分:3)

使用curl通过php订阅。

$apikey     = 'xxxxxxxxxx'; //your apikey
$listId     = 'xxxxxxxxxx';  // your list id
$endpoint   = "http://yourdatacenter.api.mailchimp.com/3.0/lists/";      // find your datacenter in your apikey( xxxxxxxxxxxxxxxxxxxxxxxx-us13 <= this is your datacenter)
$auth       = base64_encode( 'user:'. $apikey );
$data       = array(
                    'apikey'        => $apikey,
                    'email_address' => 'yourvalid_email_address',
                    'status'        => 'subscribed',
                    'merge_fields'  => array());

$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint.$listId.'/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);

echo "<pre>";  // Response form mailchimp
print_r(json_decode($result,true));

答案 4 :(得分:0)

以下示例可能对某些人有帮助。

mailchimp subscriber api example