【问题标题】:Mailchimp v3.0 API, using Perl CurlMailchimp v3.0 API,使用 Perl Curl
【发布时间】:2016-01-11 02:47:01
【问题描述】:

我正在尝试找到一种方法来向 MailChimps 新的 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 请求的建议持开放态度......但我对此所做的一切尝试都失败了,MailChimp 返回了“内部服务器错误”,这不是很有帮助)

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

  • 在 DNS 缓存中找不到主机名
  • 正在尝试 184.86.100.251...
  • 连接到 us6.api.mailchimp.com (184.86.100.251) 端口 443 (#0)
  • 成功设置证书验证位置:
  • CAfile:无 CApath:/etc/ssl/certs
  • 使用 TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256 的 SSL 连接
  • 服务器证书:
  • 主题:C=US; ST=GA; L=亚特兰大; O=火箭科学集团; OU=火箭科学集团; CN=*.api.mailchimp.com
  • 开始日期:2015-09-22 14:39:14 GMT
  • 有效期:2016-09-22 14:39:13 GMT
  • subjectAltName: us6.api.mailchimp.com 匹配
  • 发行人:C=NL; L=阿姆斯特丹; O=Verizon 企业解决方案; OU=网络信任; CN=Verizon Akamai SureServer CA G14-SHA2
  • SSL 证书验证正常。
  • 使用 Basic 和用户“用户”的服务器身份验证

    PUT /3.0/lists HTTP/1.1 授权:基本 xxxx 主机:us6.api.mailchimp.com 接受:/ 内容类型:application/json 内容长度:108

  • 上传完全发送:108 个字节中的 108 个

  • 服务器nginx未列入黑名单https://us6.api.mailchimp.com/schema/3.0/ProblemDetailDocument.json; rel=" describeBy"
  • 关闭连接 0 传输正常收到响应:HTTP/1.1 405 Method Not Allowed 服务器:nginx 内容类型: 应用程序/问题+json; charset=utf-8 内容长度:253 X 请求 ID:5f6ab08f-69e7-4c9b-b22a-91714331d5b7 链接: https://us6.api.mailchimp.com/schema/3.0/ProblemDetailDocument.json; rel=" describeBy" 允许: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":"请求的方法和资源 不兼容。请参阅允许标头了解此资源的可用情况 方法。","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";
    }

我希望这可以减轻我的悲伤:)

【问题讨论】:

  • 尝试运行$curl->setopt(CURLOPT_VERBOSE, 1); 以获取详细输出。它可以帮助您进一步调试。
  • @SabujHassan - 谢谢,我已经更新了上面的问题,包括调试(已取出身份验证密钥)
  • @AndrewNewby 我只是过来说声谢谢,我爱你。这为我节省了几天手动创建解决方案的工作量。
  • @ljamison 不用担心 - 我很高兴我的痛苦救了别人一些 =) 呵呵

标签: perl curl mailchimp


【解决方案1】:

您正在尝试连接到 $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);

【讨论】:

  • 谢谢!就是这样。我现在遇到了另一个错误,但会深入研究:,"errors":[{"field":"","message":"Schema describes object, array found instead"}]
  • 啊...明白了....在 JSON 变量中使用 [] 而不是 {} ...现在看起来不错(获取有关用户存在的错误,这很好,因为确实)
  • 这很奇怪——PUT 不应该为现有用户返回错误,这就是它的全部意义所在,它允许添加或更新功能。您可能需要咨询他们的 API 支持团队。
  • 是的,错误是我使用 [ ] 而不是 hashref: { } ... 所以它没有得到预期的 JSON 输出(因为它的编码方式不同)跨度>
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2018-11-30
    • 2017-06-11
    • 2015-08-02
    • 2016-05-02
    • 2017-09-01
    • 2016-05-03
    • 2017-11-23
    • 2016-07-03
    • 2016-11-24
    相关资源
    最近更新 更多