使用MailChimp API V3.0和PHP无法在列表上获取现有订户

时间:2016-12-31 14:32:05

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

我试图构建一个下载库,用户通过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没有发出任何消息......试过不知道出了什么问题......

1 个答案:

答案 0 :(得分:0)

最后让它工作看到完整的源代码

export class ResultCardComponent {
   @Input() public result: Result;
   @Output() public onResultClicked: EventEmitter<Result> = new EventEmitter<Result>();
   public paramsSub: any;
   public selectedId: any;

   private resultId: string;

   constructor(
      private route: ActivatedRoute,
      private router: Router,
   ) { }

   public emitResult() {
      this.onResultClicked.emit(this.result);
      this.paramsSub = this.route.params.subscribe((params) => this.selectedId = params["id"]);
      this.router.navigate(["find", this.result.id]);//only doing this as a hack workaround.
   }
}

action.php代码

   public emitResult() {
      //this.onResultClicked.emit(this.result);
      this.route.params['id'] = this.result.id;
   }

请参阅Demo