【发布时间】: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 不用担心 - 我很高兴我的痛苦救了别人一些 =) 呵呵