Mailchimp v3.0 API,使用 Perl Curl

Mailchimp v3.0 API, using Perl Curl

我正在尝试找到一种方法来向 MailChimps new API v3.0 发出 Curl 请求,这将使用户订阅给定列表。这是我到目前为止所拥有的:

use warnings;
use WWW::Curl::Easy;
use JSON;

my $apikey = 'xxxx';
my $listid = 'xxxx';
my $email = 'andy@test.co.uk';
my $endpoint = "https://us6.api.mailchimp.com/3.0/lists";

my $json = JSON::encode_json([
    'email_address' => $email,
    'status'        => 'pending',
    'merge_fields'  => [
        'FNAME'     => "andy",
        'LNAME'     => "newby"
    ]
]);

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, $endpoint);

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

my $response_body;
$curl->setopt(CURLOPT_WRITEDATA,$response_body);

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

# 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";
}

文档非常稀缺,因为它是新的 API。有没有人在 MailChimp v3 API 上成功订阅了 Perl 中的某人? (我也对命令行 curl 请求的建议持开放态度......但是我尝试过的一切都失败了 "internal server errors" 从 MailChimp 返回,这不是很有帮助)

更新: 如下所示,我启用了 verbose,它现在吐出:

  • Hostname was NOT found in DNS cache
  • Trying 184.86.100.251...
  • Connected to us6.api.mailchimp.com (184.86.100.251) port 443 (#0)
  • successfully set certificate verify locations:
  • CAfile: none CApath: /etc/ssl/certs
  • SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
  • Server certificate:
  • subject: C=US; ST=GA; L=Atlanta; O=ROCKET SCIENCE GROUP; OU=Rocket Science Group; CN=*.api.mailchimp.com
  • start date: 2015-09-22 14:39:14 GMT
  • expire date: 2016-09-22 14:39:13 GMT
  • subjectAltName: us6.api.mailchimp.com matched
  • issuer: C=NL; L=Amsterdam; O=Verizon Enterprise Solutions; OU=Cybertrust; CN=Verizon Akamai SureServer CA G14-SHA2
  • SSL certificate verify ok.
  • Server auth using Basic with user 'user'

    PUT /3.0/lists HTTP/1.1 Authorization: Basic xxxx Host: us6.api.mailchimp.com Accept: / Content-Type: application/json Content-Length: 108

  • 上传完全关闭:108 个字节中有 108 个 < HTTP/1.1 405 方法不允许

  • 服务器nginx未列入黑名单<服务器:nginxhttps://us6.api.mailchimp.com/schema/3.0/ProblemDetailDocument.json; rel="describedBy" < 允许:GET,POST < 日期:2015 年 10 月 13 日,星期二 11:24:32 GMT < 连接:关闭 < Set-Cookie:_AVESTA_ENVIRONMENT=prod;路径=/ <
  • 正在关闭连接 0 传输正常收到响应:HTTP/1.1 405 方法不允许服务器:nginx Content-Type: application/problem+json;字符集=utf-8 Content-Length: 253 X-Request-Id: 5f6ab08f-69e7-4c9b-b22a-91714331d5b7 Link: https://us6.api.mailchimp.com/schema/3.0/ProblemDetailDocument.json; rel="describedBy" 允许:GET,POST 日期:2015 年 10 月 13 日,星期二 11:24:32 GMT 连接:关闭 Set-Cookie: _AVESTA_ENVIRONMENT=prod;路径=/

{"type":"http://kb.mailchimp.com/api/error-docs/405-method-not-allowed","title":"方法 Not Allowed","status":405,"detail":"请求的方法和资源 不兼容。请参阅允许 header 了解此资源的可用情况 方法。","instance":""}

虽然我不太确定该怎么做:/

工作代码:感谢 TooMuchPete,我设法让它继续运行。对于在 Perl 中尝试使用 MailChimp API (3.0) 时可能遇到此问题的任何人,下面是一个工作示例(您只需要替换电子邮件、名称、api 键的值,和列表 ID);

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

    my $apikey = 'xxxx-us6';
    my $listid = 'xxxx';
    my $email = 'andy@testr.co.uk';
    my $endpoint = "https://us6.api.mailchimp.com/3.0/lists";

    my $json = JSON::encode_json({
        'email_address' => $email,
        'status'        => 'pending',
        'merge_fields'  => {
            'FNAME'     => "andy",
            'LNAME'     => "newby"
        }
    });

    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']);
    $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;

    # Looking at the results...
    if ($retcode == 0) {
            print("Transfer went ok\n");
            my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
            # judge result and next action based on $response_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";
    }

我希望这能减轻我的悲痛:)

您正在尝试连接到 $endpoint 而不是 $url

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

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

应该是:

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

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

在我切换到 md5_base64() 调用之前,我使用上面的代码收到了来自 MailChimp 的非法字符响应。