簡體   English   中英

使用MailChimp API V3.0和PHP無法在列表上獲取現有訂戶

[英]Cannot get existing subscriber on List using MailChimp API V3.0 and PHP

我正在嘗試建立一個下載庫,以便用戶通過MailChimp進行訂閱,以便他們可以作為成員下載文件。我想在生成下載鏈接之前確認用戶電子郵件,否則將用戶輸入的新電子郵件添加到郵件列表中,我嘗試使用此代碼來收到現有電子郵件,但沒有任何反應,請告訴我我錯了。

<?php
session_start();
if(isset($_POST['submit'])){
    $email = $_POST['email'];
    if(!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL) === false){
        // MailChimp API credentials
        $apiKey = '*******************';
        $listID = '**********';

        // 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'
        ]);

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


        // store the status message based on response code
        if ($httpCode == 200) {
        $_SESSION['msg'] = '<p style="color: #34A853">You have 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: #EA4335">'.$msg.'</p>';
        }
    }else{
        $_SESSION['msg'] = '<p style="color: #EA4335">Please enter valid email address.</p>';
    }
}
// redirect to homepage
header('location:download.php');

案例214沒有發出任何消息...嘗試不知道出了什么問題。

終於奏效了,查看完整的源代碼

<?php
session_start();
$statusMsg = !empty($_SESSION['msg'])?$_SESSION['msg']:'';
unset($_SESSION['msg']);
?>

    <h2>Subscribe Our Newsletter</h2>
    <?php echo $statusMsg; echo @$msg; ?>
    <form method="post" action="action.php">

        <p><label>Email: </label><input type="text" name="email" /></p>
        <p><input type="submit" name="submit" value="SUBSCRIBE"/></p>
    </form>

action.php代碼

        // member information
$arr = array('email_address' => $email, 'status' => '');
$json = json_encode($arr);

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


        // store the status message based on response code
        if ($json->{'status'} == 400) {
                $_SESSION['msg'] = ' <div id="mc_embed_signup_scroll">
    <div class="formstatus error">You have not subscribe to our mail List!</div>';  

        } elseif($json->{'status'} == 'subscribed') {
            if ($num) {
               $_SESSION['msg'] = '<div class="formstatus" style="color: #EA4335"><a href="'.$url.'">Download Now</a></div>';
            }
            else
            {
               $_SESSION['msg'] = '<div class="formstatus error">This file does not exist!</div>';     
            }

        }
    }else{
        $_SESSION['msg'] = '<p style="color: #EA4335">Please enter valid email address.</p>';
    }
}
// redirect to homepage
header('location:download.php?file='.$file_id.' '); 

觀看演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM