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

时间:2017-04-18 06:33:03

标签: php wordpress forms mailchimp mailchimp-api-v3.0

我在表单后使用了这个。但是我的条目没有转到mailchimp帐户。如果有任何遗漏,请告诉我。请参考以下代码。

  void Begin::atm() //The main function that makes it easier for threads to 
  call and run the Program.
{
    ATM atm;
    int choice, amount;
    LARGE_INTEGER cicles;
    QueryPerformanceCounter(&cicles);
    srand(cicles.QuadPart);

    for (int i = 0; i < imax; i++) //mimics a total of 5 customers
    {
        rw.ReadLock(); //Have to place to read lock here.
        choice = rand() % 2; //Randomizes the choice of depositing or withdrawing.
        amount = rand() % 5000 + 1; //Randomizes 'the amount of cash that the customers use.
        rw.ReadUnlock(); //Read unlock must happen here otherwise it blocks the writers.

        rw.WriteLock(); //Must happen here!
        if (choice == 0)
        {
            atm.cashdp(amount);
            cout << "\tCustomer depositing $" << amount << endl;
        }
        else if (choice == 1)
        {
            atm.cashwd(amount);
            cout << "\tCustomer withdrawing $" << amount << endl;
        }
        else
            //error checker against the randomizer for the choice of depsoiting or withdrawing.
            cout << "error rand creating wrong number" << endl;
        rw.WriteUnlock(); //Must Happen here!
        Sleep(5000); // Sleeps the program between customer usage to mimic actual use.

    }
}

4 个答案:

答案 0 :(得分:0)

尝试使用单引号添加列表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>";
    }
?>

答案 1 :(得分:0)

在不使用sdk类

的情况下集成mailchimp表单
 $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

答案 2 :(得分: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' );

答案 3 :(得分:0)

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

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

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

免责声明:我隶属于 zzBots