无法让 MailChimp 使用带有 Curl 的 API 保存我的数据
Posted
技术标签:
【中文标题】无法让 MailChimp 使用带有 Curl 的 API 保存我的数据【英文标题】:Can not get MailChimp to save my data using the API with Curl 【发布时间】:2014-10-23 00:38:07 【问题描述】:我已经尝试并尝试使用 curl 向 MailChimp 发送数据,但无法获取 要保存在 MailChimp 中的数据。对此的任何帮助将不胜感激!
这是我的代码:
$mailChimpUrl = "http://us2.api.mailchimp.com/1.3/?method=listSubscribe";
$merges = array(
'FNAME'=>'Dave',
'LNAME'=>'Gilmour',
'BUILDING'=>'Central High School',
'MMERGE17' => '35904',
'MMERGE12'=>'Yes'
);
$apikey="myrealapiishere-us2";
$listId="myrealformidishere";
$email="zz22@aol.com";
$double_optin=true;
$update_existing=false;
$replace_interests=true;
$send_welcome=false;
$email_type = 'html';
$data = array(
'email_address'=>$email,
'apikey'=>$apikey,
'merge_vars' => $merges,
'id' => $listId,
'double_optin' => $double_optin,
'update_existing' => $update_existing,
'replace_interests' => $replace_interests,
'send_welcome' => $send_welcome,
'email_type' => $email_type
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $mailChimpUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
curl_close($ch);
【问题讨论】:
mailchimp 返回什么? 首先,我建议您使用最新的 2.0 API... 类似的结构,但它们简化了一些事情。 第二,切勿将未加密的连接用于任何 API 集成。 【参考方案1】:这里是简单的 MailChimp php API 3.0 Curl 示例代码 sn-p
<?PHP
// put your api key here note the ending text past the - this is your datacenter
// the datacenter needs to be added into to the url in the curlopt_url (see below)
$apikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-us11"; // my datacenter was "us11"
// listid goes here - to find this... log into mail chimp go to Lists menu ,
// look to far right of list name for a drop down arrow, select the "Settings" dropdown,
// scroll to bottom and look for "Unique id for list"
$list_id = "xxxxxxxxxx"; // web site list
// the data I used to register (there may be others you can use, check API docs)
$email = "<<email_address_to_register>>";
$fname = "<<first_name>>";
$lname = "<<last_name>>";
$auth = base64_encode( 'user:'.$apikey );
// Notice the value of 'status' is 'pending'
// I found this via a google search indicating a double opt in subscription process
$data = array(
'apikey' => $apikey,
'email_address' => $email,
'status' => 'pending',
'merge_fields' => array(
'FNAME' => $fname,
'LNAME' => $lname,
)
);
$json_data = json_encode($data);
$ch = curl_init();
// notice datacenter "us11" comes after the // - make sure you update this to your datacenter (e.g. us2, us7 etc) or you'll get the "wrong datacenter" error.
$curlopt_url = "https://us11.api.mailchimp.com/3.0/lists/$list_id/members/";
curl_setopt($ch, CURLOPT_URL, $curlopt_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
/*
// some debug statements
var_dump($result);
print_r ($result);
*/
// here is simple way to determine status of a subscription
// $result is in JSON format
// this following loop is a simple JSON decode loop I found via google
$status = "undefined";
$msg = "unknown error occurred";
$myArray = json_decode($result, true);
foreach($myArray as $key => $value)
// debug key<<< = >>>$value<<< <br>";
if( $key == "status" )
$status=$value;
//debug echo" status found $status<Br>";
else if ($key == "title")
$msg=$value;
//debug echo" title found $msg<Br>";
// create the output that gets displayed or returned if invoked by AJAX method
if( $status == "pending" )
$msg = "Success! <br>$email has been subscribed <Br>check your inbox for the confirmation email to complete your subscription";
else
$msg = "Sorry can not subscribe email $email <br>$msg <Br>";
echo "$msg <br>";
die(' '); // frees up mem etc..
?>
【讨论】:
它给了我这个错误 [type] => developer.mailchimp.com/documentation/mailchimp/guides/… [title] => JSON Parse Error [status] => 400 [detail] => 我们遇到了一个未指定的 JSON 解析错误。 这现在应该是公认的答案,因为 MailChimp API 的第 2 版即将停用。 有人能解释一下为什么mailchimp 开发者没有提供这样一个易于使用的例子吗?我想那会很容易......【参考方案2】:正如我在评论中提到的,您应该考虑最新的 2.0 API。除此之外,这是我在生产环境中使用的代码。
虽然杂乱无章,但它很实用。只需将 merge_vars 和变量替换为您的变量即可
所有$lead
变量都被拉到脚本的其他地方......与此无关。你应该仍然能够得到这个想法。 ;)
如果某些内容仍未保存,则说明您在某处有错字。检查一切。我花了一个小时才意识到我拼错了'merge_vars'
。
$merge_vars=array(
'OPTIN_IP'=>$ip, // Use their IP (if avail)
'OPTIN-TIME'=>"now", // Must be something readable by strtotime...
'FNAME'=>ucwords(strtolower(trim($lead['first_name']))),
'LNAME'=>ucwords(strtolower(trim($lead['last_name']))),
'COMPANY'=>ucwords(strtolower(trim($lead['company']))),
'ORGTYPE'=>ucwords(strtolower(trim($lead['company_type']))),
'PLANNING'=>strtolower(trim(empty($lead['planning_stage'])?"Unknown":$lead['planning_stage'])),
);
$send_data=array(
'email'=>array('email'=>$lead['email']),
'apikey'=>"", // Your Key
'id'=>"", // Your proper List ID
'merge_vars'=>$merge_vars,
'double_optin'=>false,
'update_existing'=>true,
'replace_interests'=>false,
'send_welcome'=>false,
'email_type'=>"html",
);
$payload=json_encode($send_data);
$submit_url="https://us4.api.mailchimp.com/2.0/lists/subscribe.json";
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$submit_url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$payload);
$result=curl_exec($ch);
curl_close($ch);
$mcdata=json_decode($result);
if (!empty($mcdata->error)) return "Mailchimp Error: ".$mcdata->error;
return ""; // <-- This was obviously from within a function. If you made it here, it was a success
【讨论】:
非常感谢@Oberst!在你提到它之前,我没有遇到任何关于 2.0 API 的东西。我最初使用的那个来自这里:http://apidocs.mailchimp.com/api/1.3/listsubscribe.func.php
一旦我转换为那个并将email_address
更改为email
并使用array
来表示它的工作价值:) 再次感谢!【参考方案3】:
对于任何寻求更新方法的人来说,有一个非常好的包; https://github.com/spatie/laravel-newsletter
您可以通过composer安装它,设置和使用非常简单:
use Newsletter;
Newsletter::subscribe('test@mail.com');
【讨论】:
【参考方案4】:以前的答案不起作用,我必须调整它们,这里是有效的:
<?PHP
$apikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx-us20"; // found in Account -> extra
$list_id = "xxxxxxxxx"; // found in Audience -> audience id
$email = $_POST["email"];
$data = array(
'apikey' => $apikey,
'email_address' => $email,
'status' => 'subscribed',
);
// Setup cURL
$ch = curl_init('https://us20.api.mailchimp.com/3.0/lists/'.$list_id.'/members/');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Authorization: apikey '.$apikey,
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => json_encode($data)
));
$response = curl_exec($ch);
$status = "undefined";
$msg = "unknown error occurred";
$myArray = json_decode($response, true);
foreach($myArray as $key => $value)
if( $key == "status" )
$status=$value;
else if ($key == "title")
$msg=$value;
if( $status == "subscribed" )
$msg = "Success";
else
$msg = "Sorry can not subscribe email $email <br>$msg <Br>";
echo "$msg";
die(' '); // frees up mem etc..
?>
【讨论】:
以上是关于无法让 MailChimp 使用带有 Curl 的 API 保存我的数据的主要内容,如果未能解决你的问题,请参考以下文章
无法连接到 us15.api.mailchimp.com 端口 443:超时
仅限于通过 R 和 cURL 使用 Mailchimp API v3 提取详细信息(如电子邮件活动)?
无法在 node.js 中交换访问令牌的代码。 Mailchimp API