duanqiang2617 2016-11-06 14:12
浏览 46

资源Mailchimp API无效

Using to a CURL script to add a new entry to a Mailchimp mailing list using their API but get an invalid resource error. I can see the three fields in the mailing list namely EMAIL, FNAME & LNAME so cannot see why there would be a validation error. How could I troubleshoot this better so I can see the exact cause of the error?

<?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 = "xxxxxxxxxxxxxxxxxxxxxxx-us13"; // 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 = "xxxxxxxx"; // web site list


// the data I used to register (there may be others you can use, check API docs)
$email = "test@test.com";
$fname = "Test";
$lname = "Test";


$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://us13.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(' ');
?>
  • 写回答

1条回答 默认 最新

  • dougutuo9879 2017-05-25 14:55
    关注

    Try this

    $data = ([
        'apikey'        => $apikey,
        'email_address' => $email,
        'status'        => 'pending',
        'merge_fields'  => array(
        'FNAME' => $fname,
        'LNAME' => $lname,
    ]);
    
    $json_data = json_encode($data);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分