繁体   English   中英

如何使用curl和php将电子邮件注册到MailChimp邮件列表

[英]How to sign up email to MailChimp mailing list using curl and php

我正在尝试将注册我的WordPress网站的用户注册到MailChimp邮件列表。 这是我正在使用的功能:

function rb_signup_mailing_list($email, $username, $fname, $lname){
$api_key        =   '<my_api_key>';
$datacenter     =   '<my_data_center>';
$list_id        =   '<my_list_id>';
$auth           =   base64_encode('user:'. $api_key);
$member_id      =   md5(strtolower($email));
$status         =   'subscribed';
$merge_fields   = ['FNAME' => $fname, 'LNAME' => $lname];

$data = array(
    //'apikey'      =>  $api_key,
    'email_address' =>  $email,
    'status'        =>  $status,
    'merge_fields'  =>  $merge_fields,
);
$data_string = json_encode($data);

//$url = 'https://' . substr($api_key, strpos($api_key, '-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' .md5(strtolower($email));
//$url = 'https://' . substr($api_key, strpos($api_key, '-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/';
$url = 'https://' . $datacenter . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/';


$ch = curl_init($url);
// curl_setopt($ch, CURLOPT_URL, $url);
// curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, 
    array(
        'Content-Type: application/json',
        'Authorization: Basic ' . $auth,
        //'Content-Length: '    . strlen($data_string),
    )
);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datastring);



$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

}

我这样调用该函数:

rb_signup_mailing_list('myemail@example.com', 'my_username', 'Hans', 'Meyer');

$ result在curl_exec之后包含以下内容:

{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Invalid Resource","status":400,"detail":"The resource submitted could not be validated. For field-specific details, see the 'errors' array.","instance":"","errors":[{"field":"","message":"Schema describes object, NULL found instead"}]}

我可以在MailChimp中看到请求,但是它们没有将电子邮件添加到列表中。 我究竟做错了什么?

您在声明变量$data_string来保存订户数据,但是在CURL请求中使用了$datastring 尝试将它们更新为相同的名称,并且应该可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM