无法使MailChimp使用带有Curl的API来保存我的数据 [英] Can not get MailChimp to save my data using the API with Curl

查看:62
本文介绍了无法使MailChimp使用带有Curl的API来保存我的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试并尝试使用curl将数据发送到MailChimp,但无法获取 数据保存在MailChimp中.任何帮助,将不胜感激!

I have tried and tried to send data to MailChimp using curl but cannot get the data to save in MailChimp. Any help with this would be greatly appreciated!

这是我的代码:

$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);

推荐答案

正如我在评论中提到的那样,您应该考虑使用最新的2.0 API.除此之外,这是我在生产环境中使用的代码.

As I mentioned in my comment, you should consider the latest 2.0 API. Aside from that, this is code I am using in a production environment.

尽管杂乱无章,但却是实用的.只需将merge_vars和变量替换为您的变量即可 所有$lead变量都将被拉到脚本的其他位置...与此无关.您仍然应该能够理解这个想法. ;)

Albeit messy, it is functional. Just replace the merge_vars and variables with what yours are All the $lead variables are being pulled elsewhere in the script... Not relevant to this. You should still be able to get the idea. ;)

如果仍然没有保存某些内容,那么您在某处有错字.检查一切.一次花了我一个小时才意识到我拼错了'merge_vars'.

If something still isn't being saved, then you have a typo somewhere. Check EVERYTHING. Took me an hour once to realize I had misspelled '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

这篇关于无法使MailChimp使用带有Curl的API来保存我的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆