【问题标题】:Mailchimp http 200 when already subscribedMailchimp http 200 已订阅
【发布时间】:2019-07-30 07:21:05
【问题描述】:

虽然我找到了一些关于这个问题的线索,但我仍然无法得到最终的解决方案。 为什么我不断收到 http 结果代码 200,即使电子邮件地址已经在列表中。因此没有返回代码 214。 现在数据正在覆盖现有数据,我想避免这种情况。 谢谢你

session_start();
if(isset($_POST['submit'])){
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    if(!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL) === false){
        // MailChimp API credentials
        $apiKey = 'xxxxxxxxxxxxx';
        $listID = 'xxxxxxx';
        
        // MailChimp API URL
        $memberID = md5(strtolower($email));
        $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
        $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listID . '/members/' . $memberID;
        
        // member information
        $json = json_encode([
            'email_address' => $email,
            'status'        => 'subscribed',
            'merge_fields'  => [
                'FNAME'     => $fname,
                'LNAME'     => $lname
            ]
        ]);
        
        // send a HTTP POST request with curl
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        $result = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        $_SESSION['code'] = $httpCode;
        // store the status message based on response code
        if ($httpCode == 200) {
            $_SESSION['msg'] = '<p style="color: red">You have successfully subscribed.</p>';
        } else {
            switch ($httpCode) {
                case 214:
                    $msg = 'You are already subscribed.';
                    break;
                default:
                    $msg = 'Some problem occurred, please try again.';
                    break;
            }
            $_SESSION['msg'] = '<p style="color: red">'.$msg.'</p>';
        }
    }else{
        $_SESSION['msg'] = '<p style="color: #EA4335">Please enter valid email address.</p>';
    }
}
// redirect to prev page
header('location:index.php');

【问题讨论】:

  • 您确定要在请求网址的末尾添加$memberID 吗?我对 mailchimp api 没有太多经验,但仅从检查文档看来,在最后添加 userID 会进行状态检查,而忽略它实际上会将其添加到列表中。 developer.mailchimp.com/documentation/mailchimp/guides/…
  • 还要确保您实际上是在发送 POST 请求,方法是添加:curl_setopt($ch, CURLOPT_POST, 1);
  • 你好德克,湿漉漉的。我尝试了两种选择。删除 '$memberID' 会给出 405(不允许的方法);第二个对结果没有影响。我不断收到“200”服务器响应。将执行一些其他测试。
  • 多花一点时间研究 Dirk Scholten 是对的。他的言论起到了作用。现在可以了。谢谢。
  • @numbernine 你能分享一下你更新的代码吗?如上所述,我尝试添加 curl_setopt($ch, CURLOPT_POST, 1);,但我仍然收到 200 份重复电子邮件的成功回复。

标签: php json mailchimp-api-v3.0


【解决方案1】:
<?php
session_start();
if(isset($_POST['submit'])){
$email = $_POST['email'];

if(!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL) === false){
    // MailChimp API credentials
    $apiKey = 'xxxxxxxxxxxxx';
    $listID = 'xxxxxxx';
    
    // MailChimp API URL
    $memberID = md5(strtolower($email));
    $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
    $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listID . '/members/' . $memberID;
    
    // member information
    $json = json_encode([
        'email_address' => $email
       ]);
            // send a HTTP POST request with curl
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
 //mod start
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ch, CURLOPT_POST, 1);
 //mod end
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
   $_SESSION['code'] = $httpCode;
    // store the status message based on response code
    if ($httpCode == 200) {
        $_SESSION['msg'] = '<p style="color: #34A853">E-mail address already on list. ('.$_SESSION['code']. ')</p>';
    } else {
        switch ($httpCode) {
            //case 214:
            case 404:
                $msg = 'E-mail address not yet on list. ('.$_SESSION['code']. ')</p>';
                break;
            default:
                $msg = 'Error. Pls try again. ('.$_SESSION['code']. ')</p>';
                break;
        }
        $_SESSION['msg'] = '<p style="color: #EA4335">'.$msg.'</p>';
    }
}else{
    $_SESSION['msg'] = '<p style="color: #EA4335">Please enter valid email address.</p>';
}
}
// redirect to prev page
echo $httpCode;
//echo $result ;
//echo $url;
header('location:add.php');

【讨论】:

  • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票。
猜你喜欢
  • 2017-11-21
  • 2016-01-01
  • 2013-11-09
  • 2015-08-13
  • 2015-06-02
  • 2016-02-17
  • 2016-05-11
  • 2017-08-11
  • 2014-05-24
相关资源
最近更新 更多