Mailchimp注册表突然停止工作

时间:2017-12-19 22:28:37

标签: javascript php jquery ajax mailchimp

我在客户的网站上有一个Mailchimp注册表格,直到本周工作正常,我不知道发生了什么,但当我提交新的电子邮件地址时,它返回一个我在调试工具中看不到的错误

我不知道它是否与代码有关,或者如果Mailchimp对他们的系统进行了更改,但这里是用于注册表单的代码,我没有制作这个插件所以我不知道是否有什么不见了。

HTML

<form action="" class="c-mailchimp">
    <input type="text" class="mce-validator" value="" style="display: none;">
    <input type="email" class="with-button mce-EMAIL" placeholder="Votre courriel" required>
    <button type="submit" class="stick-to-input mc-embedded-subscribe">
        <i class="fa fa-paper-plane-o" aria-hidden="true"></i>
    </button>
    <div class="mce-responses">
        <div class="c-mailchimp__response mce-error-response" style="display:none">Oups, une erreur est survenue..</div>
        <div class="c-mailchimp__response mce-success-response" style="display:none">Merci de vous être inscrit !</div>
    </div>
</form>

自定义-mce.php

<?php
/*
   Plugin Name: Agence CC
   Plugin URI: http://wordpress.org/extend/plugins/agence-cc/
   Version: 1
   Author: Agence CC
   Description: 
   Text Domain: agencecc
   License: GPLv3
  */

add_action( 'wp_ajax_submitMailchimpNewUser', 'submitMailchimpNewUser' );
add_action( 'wp_ajax_nopriv_submitMailchimpNewUser', 'submitMailchimpNewUser' );

function submitMailchimpNewUser() {

    require_once 'includes/mailchimp.php';

    $newUserEmail = $_POST['email'];

    $subscribeResult = syncMailchimp($apiKey, $listId, $newUserEmail);

    echo $subscribeResult;

    die();

}

?>

mailchimp.php

<?php

$apiKey = 'Hidden for security purpose';
$listId = 'Hidden for security purpose';

function syncMailchimp($apiKey, $listId, $email) {

    $memberId = md5(strtolower($email));
    $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);

    $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/'.$memberId;

    $json = json_encode([
        'email_address' => $email,
        'status' => "subscribed",
    ]);

    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Content-Length: ' . strlen($json);

    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $curlInfo = curl_getinfo($ch);

    curl_close($ch);

    if($httpCode===200) {
        return 'true';
    } else {
        return 'false';
    }
}

在app.js内部

var initMailChimp = function() {

    var $submitButton = $('.mc-embedded-subscribe');

    $submitButton.click(function(event){
        event.preventDefault();

        var $email = $(this).closest('form').find('.mce-EMAIL');
        var email = $email.val();
        var success = $(this).closest('form').find('.mce-success-response');
        var error = $(this).closest('form').find('.mce-error-response');
        var validator = $(this).closest('form').find('.mce-validator').val();

        if(validator!=='') {

            error.hide();
            success.slideDown(300);
            $email.val('');

        } else {

            $.post(
                ajaxurl, 
                {
                    'action': 'submitMailchimpNewUser',
                    'email': email
                }, 
                function(response){
                    if(response==='true') {
                        error.hide();
                        success.slideDown(300);
                        $email.val('');
                    } else {
                        success.hide();
                        error.slideDown(300);
                    }
                }
            );
        }
    });
};

0 个答案:

没有答案