无法获取MailChimp API Curl工作 [英] Can't get MailChimp API Curl to work

查看:193
本文介绍了无法获取MailChimp API Curl工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图并尝试使用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 API Curl工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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