[英]Create campaign with dynamic segment using MailChimp API V3.0
[英]Mailchimp Subscribe using API v3.0
我正在嘗試將電子郵件訂閱到PHP中的MailChimp列表。 我實際上不是后端開發人員,所以我對此停滯不前:(
我正在使用MailChimp幫助程序PHP庫: https : //github.com/drewm/mailchimp-api
我已經搜索了所有互聯網,並且只能得到500個內部服務器錯誤的狀態。 我已經在生產服務器中。
<?php
include("./inc/MailChimp.php");
use \DrewM\MailChimp\MailChimp;
$api_key = "xxxxxxxxxxx-us13";
$list_id = "7xxxxxxx4";
$MailChimp = new MailChimp($api_key);
$result = $MailChimp->post("lists/$list_id/members", [
"email_address" => $_POST["txt_mail"],
'merge_fields' => ['FNAME'=>$_POST["txt_name"], 'FPHONE'=>$_POST["txt_phone"], 'FMSG'=>$_POST["txt_message"]],
"status" => "subscribed"
]);
if ($MailChimp->success()) {
echo "<h4>Thank you, you have been added to our mailing list.</h4>";
} else {
echo $MailChimp->getLastError();
} ?>
哦,您沒有想法,當我遇到這個問題時,我感到多么沮喪。
幸運的是,我發現Misha Rudrastyh的這一方便功能與API 3.0配合得非常好。 這是要點:
自從我使用Wordpress以來,我首先將以下代碼放入了我的functions.php
文件中(在這里使用您的變量進行編輯)
<?php
function rudr_mailchimp_subscriber_status( $email, $status, $list_id, $api_key, $merge_fields = array('FNAME'=> '', 'FPHONE'=> '', 'FMSG'=> '') ){
$data = array(
'apikey' => $api_key,
'email_address' => $txt_mail,
'status' => $status,
'merge_fields' => $merge_fields
);
$mch_api = curl_init(); // initialize cURL connection
curl_setopt($mch_api, CURLOPT_URL, 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($data['email_address'])));
curl_setopt($mch_api, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.base64_encode( 'user:'.$api_key )));
curl_setopt($mch_api, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($mch_api, CURLOPT_RETURNTRANSFER, true); // return the API response
curl_setopt($mch_api, CURLOPT_CUSTOMREQUEST, 'PUT'); // method PUT
curl_setopt($mch_api, CURLOPT_TIMEOUT, 10);
curl_setopt($mch_api, CURLOPT_POST, true);
curl_setopt($mch_api, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($mch_api, CURLOPT_POSTFIELDS, json_encode($data) ); // send data in json
$result = curl_exec($mch_api);
return $result;
}
然后,我在表單過程中添加了變量字段:
<?php
$email = $_POST['txt_mail'];
$FNAME=$_POST['txt_name'];
$FPHONE=$_POST['txt_phone'];
$FMSG=$_POST['txt_message'];
$status = 'pending'; // "subscribed" or "unsubscribed" or "cleaned" or "pending"
$list_id = 'xxxxxxxxxxx-us13'; // where to get it read above
$api_key = 'xxxxxxxxxxx-us13'; // where to get it read above
$merge_fields = array('FNAME' => $FNAME, 'FPHONE' => $FPHONE, 'FMSG' => $FMSG);
rudr_mailchimp_subscriber_status($email, $status, $list_id, $api_key, $merge_fields );
?>
我希望這有幫助。 我為此苦了一段時間,直到意識到如何正確地做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.