mailchimp api 2.0 通过 php 订阅?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18427405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:33:06  来源:igfitidea点击:

mailchimp api 2.0 subscribe through php?

phpmailchimp

提问by TheGodWings

I need an example of how to subscribe a email address to mailchimp newsletter.

我需要一个关于如何将电子邮件地址订阅到 mailchimp 时事通讯的示例。

Please check new api link here: https://bitbucket.org/mailchimp/mailchimp-api-php

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

This is new malichimp api and I am not sure how to use it. :(

这是新的malichimp api,我不确定如何使用它。:(

For MailChimp 2.0 API, not for 1.3.

对于 MailChimp 2.0 API,而不是 1.3。

Please somebody provide an example on how to subscribe user to mailchimp.

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

Thank You.

谢谢你。

Edit1: Already tried following code, but not working:

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);

But not working. Throwing following error: Fatal error: Call to a member function call() on a non-object in subscribe.php on line 22

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

回答by Anis Marrouchi

Referring to the documentation, this should be like so:

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

$merge_vars = array('MM1'=>$mm1);
$listid = 'YOURLISTID';
$MailChimp = new Mailchimp($apikey);
$result = $MailChimp->lists->subscribe($listid,
                                        array('email'=>"[email protected]"),
                                        $merge_vars,
                                        false,
                                        true,
                                        false,
                                        false
                                       );
    print_r($result);

Tested and working.

测试和工作。

回答by saba

this might be helpful for some, simple mailchimp subscriber API code sample with php

这可能对使用 php 的一些简单的 mailchimp 订阅者 API 代码示例有所帮助

mailchimp subscriber API code example in PHP

PHP 中的 mailchimp 订阅者 API 代码示例

回答by l2aelba

Here is with Try & Catch (example for when dup emails)

这是使用 Try & Catch(复制电子邮件时的示例)

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

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


$email = '[email protected]';
$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()
        ));
}

Readmore about subscribe(): https://apidocs.mailchimp.com/api/2.0/lists/subscribe.php

阅读更多关于subscribe()https: //apidocs.mailchimp.com/api/2.0/lists/subscribe.php

回答by Raju Dudhrejiya

Subscribe through php using curl.

使用 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));

回答by Khandad Niazi

Here is example may it helpful for some one.

这是示例,可能对某些人有帮助。

mailchimp subscriber api example

mailchimp 订阅者 api 示例