我刚刚在MailChimp网站上阅读了以下内容:
MailChimp API v3.0现已上线! 2016年之后将不再支持早期版本,因此所有API用户都应该开始过渡到v3.0。
因此,我想转到API的v3.0。我可以在PHP中使用返回布尔值的函数,该函数将检查电子邮件地址是否订阅了特定的MailChimp列表。我不想订阅该用户,只是检查他们是否订阅。
答案 0 :(得分:8)
更新:我回答了另一个问题,其中包含更详细的教程,介绍如何使用jQuery .ajax()
进行此操作:Adding subscribers to a list using Mailchimp's API v3
查看Mailchimp documentation并假设您有一个给定的列表,看起来您将使用GET调用此端点:
/lists/{list_id}/members/{subscriber_hash}
要在PHP中执行此操作,我找到了nice script sitting on github。他们的最后一个功能可能适合你:
function mc_checklist($email, $debug, $apikey, $listid, $server) {
$userid = md5($email);
$auth = base64_encode( 'user:'. $apikey );
$data = array(
'apikey' => $apikey,
'email_address' => $email
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'.api.mailchimp.com/3.0/lists/'.$listid.'/members/' . $userid);
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_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
if ($debug) {
var_dump($result);
}
$json = json_decode($result);
echo $json->{'status'};
}
如果该函数不起作用,我可以为v3库找到的唯一包装器与Laravel - Mailchimp v3 API PHP wrapper一起使用。
答案 1 :(得分:7)
如果你使用mailchimp-api,它就像那样
include 'Mailchimp.php';
use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp('your**api***key');
function emailExistsMc($subscriberMail, $list_id){
global $MailChimp;
$subscriber_hash = $MailChimp->subscriberHash($subscriberMail);
$result = $MailChimp->get("lists/$list_id/members/$subscriber_hash");
if($result['status'] == '404') return false;
return true;
}
如果$result['status']
为404
,则找不到资源。 $result['status']
中列出了@Data //using lombok annotation to reduce code amount
@AllArgsConstructor
public class Phone {
private String countryCode;
private String areaCode;
private String dialNumber;
}
@Data
public class Fax {
@AllArgsConstructor
private String countryCode;
private String nationalPrefix;
private String areaCode;
private String dialNumber;
}
@Data
@AllArgsConstructor
public class Info {
private String name;
private String addressLine1;
private String city;
private String state;
private String zipCode;
private Phone phone;
private Fax fax;
}
public void test() {
...
given()
.when()
.contentType(ContentType.JSON)
.body(new Info("Auto Test", "4399 Apple ln", "BlahWonders", GA", "30555",
new Phone("1", "678", "3196864"),
new Fax("1", "1", "333", "3333333")))
...
}
的其他可能值:
答案 2 :(得分:2)
我使用DrewM库
function isSubscribed($emailAddress, $listId) {
$chimp = new \DrewM\MailChimp\MailChimp($apiKeyHere);
$subscriberHash = $chimp->subscriberHash($emailAddress);
$result = $chimp->get('lists/' . $listId . '/members/' . $subscriberHash);
return ($chimp->success() && isset($result['id']));
}