将语言变量添加到WWW :: Mailchimp(订阅)

时间:2015-10-06 17:39:50

标签: perl mailchimp

我正在努力弄清楚如何使用 WWW :: Mailchimp http://search.cpan.org/~arcanez/WWW-Mailchimp/)来签署我们列表中的某人,但也要指定该人的语言(即英语,法语,德语,西班牙语等)。

这是我到目前为止所做的:

my $mailchimp = WWW::Mailchimp->new(apikey => 'xxxx' );
   $mailchimp->listSubscribe( id => "xxx", email_address => $in->{Email}, merge_vars => [ FNAME => $name[0], LNAME => $name[1], mc_language => "fr", LANG => "fr", LANGUAGE => "fr" ] );

mc_language => “fr”,LANG => “fr”,LANGUAGE => “fr”似乎没有做任何事情(一直在尝试我看到周围的所有参数,徒劳地希望其中一个有效!)

虽然它有效(并要求您确认订阅),但所有语言变量都会被忽略。看看他们的文件,我对使用什么感到困惑:

https://apidocs.mailchimp.com/api/2.0/lists/subscribe.php

代码“fr”没问题,但我不确定传递给它的是什么。

以前有没有人有这方面的经验?除了语言,它工作正常(但我需要能够以他们自己的语言发送确认电子邮件,然后在邮件时过滤掉)

UPDATE:好的,所以看起来它不会是更新到新API的简单情况。我一直在研究v3.0 API,它对旧版本进行全面检查(新功能名称,发送请求的新方法等)。我要做的是研究一种“卷曲”方法,所以我们至少可以实现它。一旦我开始这样做,我可能会看一下使用LWP :: UserAgent编写代码,因为它比做大量的curl请求更清晰。遗憾的是Perl和MailChimp已经没有任何东西了(使用新的API,甚至是版本2.0!)

2 个答案:

答案 0 :(得分:2)

从查看source,默认为API 1.3:

has api_version => (
  is => 'ro',
  isa => Num,
  lazy => 1,
  default => sub { 1.3 },
);

您需要使用MC_LANGUAGE:

shows文档
  

string MC_LANGUAGE设置成员的语言首选项。支持的   代码完全区分大小写,可以找到here

看起来该模块只是将您提供的任何数据结构推送到JSON并将其POST到Mailchimp,因此应将您所针对的API的相应Mailchimp API doc版本作为主要来源引用。

答案 1 :(得分:0)

好的,所以我最终到了那里!我一直在和MailChimp支持人员谈话,他们非常乐于助人。事实证明这是一个双重问题。

1)需要为相关列表启用自动翻译。这是他们的答案:

  

看了一下这个电话后,它现在似乎已经设置好了,所以你在这方面都很好。话虽这么说,我看到了   自动翻译选项似乎没有为任何一个启用   你的清单。为了确认和所有其他响应   要自动翻译的电子邮件,需要启用此功能   正在使用的所有列表。

     

我们有一些关于此的更多信息,在这里,如果你想检查出来:   http://kb.mailchimp.com/lists/signup-forms/translate-signup-forms-and-emails#Auto-Translate-Forms

2)通过API发出请求时,您需要专门设置Accept-Language: xx值。例如,en,fr,es,de等

这是一个适用于将来需要它的人的工作功能。请务必更新apikey,listId和端点URL。

do_register_email_list('foo@bar.com','Andrew Test',"en")

sub do_register_email_list {
# (email,name,lang)

    use WWW::Curl::Easy;
    use Digest::MD5;
    use JSON;

    my @name = split /\s+/, $_[1];
    my $apikey = 'xxxx-us6';
    my $listid = 'xxxx';
    my $email = $_[0];
    my $endpoint = "https://us6.api.mailchimp.com/3.0/lists";
    my $lang = $_[2]||'en';

    my $json = JSON::encode_json({
        'email_address' => $email,
        'status'        => 'pending',
        'language' => $lang,
        'merge_fields'  => {
            'FNAME'     => $name[0]||'',
            'LNAME'     => $name[1]||''
        }
    });

    my $curl = WWW::Curl::Easy->new;

    my $url = "$endpoint/$listid/members/" . Digest::MD5::md5(lc($email));

    $curl->setopt(CURLOPT_HEADER,1);
    $curl->setopt(CURLOPT_URL, $url);

#    $curl->setopt(CURLOPT_VERBOSE, 1);
    $curl->setopt(CURLOPT_USERPWD, 'user:' . $apikey);
    $curl->setopt(CURLOPT_HTTPHEADER, ['Content-Type: application/json',"Accept-Language: $lang"]);
    $curl->setopt(CURLOPT_TIMEOUT, 10);
    $curl->setopt(CURLOPT_CUSTOMREQUEST, 'PUT');
    $curl->setopt(CURLOPT_SSL_VERIFYPEER, 0);
    $curl->setopt(CURLOPT_POSTFIELDS, $json);

    # A filehandle, reference to a scalar or reference to a typeglob can be used here.
    my $response_body;
    $curl->setopt(CURLOPT_WRITEDATA,\$response_body);

    # Starts the actual request
    my $retcode = $curl->perform;

#print "FOO HERE";
    # Looking at the results...
    if ($retcode == 0) {
        print "Transfer went ok\n";
        my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
        print "Received response: $response_body\n";
    } else {
        # Error code, type of error, error message
        print "An error happened: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n";
    }
}

希望这可以让其他人免于我对它的所有悲痛:)(MailChimp支持小姐也说她会让他们的团队在开发者笔记中添加一些关于此的东西,所以它更明确了!)