我想在不使用插件的情况下将 mailchimp 与 wordpress 集成

I want to integrate mailchimp with wordpress without using plugins

我在 form.But 我的条目没有转到 mailchimp 帐户后使用了它。请告诉我是否缺少任何东西。请参考以下代码。

<?php

// SUBSCRIBE TO MAILING LIST OPTION - ADD TO MAILCHIMP USING API
if ($_POST['emailUpdates'] == 'Yes')
{
    // Include Mailchimp API class
    require_once('MCAPI.class.php');

    // Your API Key: http://admin.mailchimp.com/account/api/
    $api = new MCAPI('xxxxxxxxx-us15');

    // Your List Unique ID: http://admin.mailchimp.com/lists/ (Click "settings")
    $list_id = "xxxxxxx";

    // Variables in your form that match up to variables on your subscriber
    // list. You might have only a single 'name' field, no fields at all, or more
    // fields that you want to sync up.
    $merge_vars = array(
        'FNAME' => $_POST['signup_first_name'],
        'LNAME' => $_POST['signup_last_name']
    );

    // SUBSCRIBE TO LIST
    if ($api->listSubscribe($list_id, $_POST['signup_user_email'], $merge_vars) === true)
    {
        $mailchimp_result = 'Success! Check your email to confirm sign up.';
    }
    else
    {
        $mailchimp_result = 'Error: ' . $api->errorMessage;
    }
}

?>

尝试用单引号添加列表 ID 这是我用于表单的示例

<?php
    require_once 'inc/MCAPI.class.php';
    $api = new MCAPI('xxxxxxxxxxx-us15');
    $merge_vars = array('FNAME'=>$_POST["fname"], 'LNAME'=>$_POST["lname"]);

    // Submit subscriber data to MailChimp
    // For parameters doc, refer to: http://apidocs.mailchimp.com/api/1.3/listsubscribe.func.php
    $retval = $api->listSubscribe( 'xxxxx', $_POST["email"], $merge_vars, 'html', false, true );

    if ($api->errorCode){
        echo "<h4>Please try again.</h4>";
    } else {
        echo "<h4>Thank you, you have been added to our mailing list.</h4>";
    }
?>

不使用 sdk 集成 mailchimp 表单 class

 $email= $_POST["email"];

$api_key = "dsfdfdsfdsdsfsfdgdfg-us15"; //api key
$list_id = "2f35dfgdgffec5c2"; // list id


  $url = 'https://us15.api.mailchimp.com/2.0/lists/subscribe.json?apikey='.$api_key.'&id='.$list_id.'&email[email]='.$email.'&double_optin=false&send_welcome=false';
echo callMailchimp($url);
function callMailchimp($url)
{
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => 2
    ));

    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

https://www.quora.com/How-do-I-use-PHP-cURL-to-access-the-new-MailChimp-API-v3-0

这是我在我的一个项目中使用的代码 我更改了一些细节,例如 id 和 api_key 如您所见,我为此制作了自定义短代码,您还将看到用户所在的位置通过 IP

填写表格

[mail_chimp_form]

<?php 
/*-------------------------------------------------------------------------------
    MAIL CHIMP API
-------------------------------------------------------------------------------*/

function rudr_mch_subscribe() {
    $list_id = 'XXXXX';
    $api_key = 'XXXXXXXX-us15';
    $ip = $_SERVER['REMOTE_ADDR'];
   $details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
    $cnt=$details->country; 
    $result = json_decode(rudr_mailchimp_subscriber_status($_POST['email'], 'subscribed', $list_id, $api_key, array('FNAME' => $firstname, 'LNAME' => $lastname),array('country_code'=>'".$cnt."')));
    // print_r($result);
            if ($resul->status == 400) {
                foreach ($result->errors as $error) {
                    echo '<p>Error: ' . $error->message . '</p>';
                }
            } elseif ($result->status == 'subscribed') {
                echo 'Thanks for Subscribing!';
            }
            die;
        }
add_action('wp_ajax_mailchimpsubscribe', 'rudr_mch_subscribe');
add_action('wp_ajax_nopriv_mailchimpsubscribe', 'rudr_mch_subscribe');
function rudr_mailchimp_subscriber_status( $email, $status, $list_id, $api_key, $merge_fields = array('FNAME' => '','LNAME' => ''),$location=array('country_code'=>'') ){
       function getUserIP()
    {
        $client  = @$_SERVER['HTTP_CLIENT_IP'];
        $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
        $remote  = $_SERVER['REMOTE_ADDR'];

        if(filter_var($client, FILTER_VALIDATE_IP))
        {
            $ip = $client;
        }
        elseif(filter_var($forward, FILTER_VALIDATE_IP))
        {
            $ip = $forward;
        }
        else
        {
            $ip = $remote;
        }

        return $ip;
    } 
    $ipAddr = getUserIP();
    $geoIP  = json_decode(file_get_contents("http://freegeoip.net/json/$ipAddr"), true);
    $lati=$geoIP['latitude'];
    $long=$geoIP['longitude'];
    $ip = $_SERVER['REMOTE_ADDR'];
    $details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
    $cnt=$details->country;
    $data = array(
        'apikey'        => $api_key,
        'email_address' => $email,
        'status'        => $status,
        'merge_fields' =>$merge_fields,
        'location' =>array(
               'latitude' =>$lati,
                'longitude' => $long,
                'country_code'=>$cnt
          ) 
    );
    $mch_api = curl_init(); // initialize cURL connection

    curl_setopt($mch_api, CURLOPT_URL, 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($data['email_address'])));
    curl_setopt($mch_api, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.base64_encode( 'user:'.$api_key )));
    curl_setopt($mch_api, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
    curl_setopt($mch_api, CURLOPT_RETURNTRANSFER, true); // return the API response
    curl_setopt($mch_api, CURLOPT_CUSTOMREQUEST, 'PUT'); // method PUT
    curl_setopt($mch_api, CURLOPT_TIMEOUT, 10);
    curl_setopt($mch_api, CURLOPT_POST, true);
    curl_setopt($mch_api, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($mch_api, CURLOPT_POSTFIELDS, json_encode($data) ); // send data in json

    $result = curl_exec($mch_api);
    return $result;
}
function mail_chimp_code(){
 $current_user = wp_get_current_user();

?>
 <div class="chimpform">
   <div class="chimpform_inenr">
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" id="mailchimp">
    <!-- for my website the site_url() function returns https://rudrastyh.com -->
    <input type="text" name="fname" placeholder="First name" style="display:none;" />
    <input type="text" name="lname" placeholder="Last name" style="display:none;" />
    <input type="email" name="email" id="mce-EMAIL" value="<?php echo $current_user->user_email ?>" placeholder="Your email address" required/>

    <input type="hidden" name="action" value="mailchimpsubscribe" />
    <!-- we need action parameter to receive ajax request in WordPress -->

    <div class="mail_left"><button>YES! I WANT TO IMPROVE MY CAREER</button></div>
    <div class="mail_right"><button class="pum-close popmake-close" type="button"/>NO THANKS, I DON'T WANT ANY HELP</button></div>
  </form>
   <p class="status"></p>
  </div>
</div>
<script>
jQuery(function($){
    $('#mailchimp').submit(function(){
        var mailchimpform = $(this);
        $.ajax({ 
            url:mailchimpform.attr('action'),
            type:'POST',
            data:mailchimpform.serialize(), 
            success:function(data){
                  $('#mailchimp').hide();
                 $('p.status').text(data);
                document.getElementById("mailchimp").reset();
            }
        });
        return false;
    });
});
</script>

<?php
}// End some_random_code()
add_shortcode( 'mail_chimp_form', 'mail_chimp_code' );

您可以使用 zzBots 设置从 WordPress 到 Mailchimp 的同步,无需任何代码或插件。

您可以在此处阅读有关此同步的更多信息:https://www.zzbots.com/community/how-to/sync-new-wordpress-users-to-mailchimp

此特定同步或集成旨在将新的 WordPress 用户作为新订阅者自动同步到 Mailchimp。虽然,您可以使用 zzBots 以许多不同的方式将这两个应用程序集成在一起。

免责声明:我隶属于 zzBots