簡體   English   中英

如何使用php中的api調用將電子郵件地址添加到mailchimp列表中

[英]how to add email address to mailchimp list using api call in php

我是mailchimp的新手。 剛才我創建了帳戶並獲得了api密鑰。 我已經通過他們的api添加電子郵件地址列表但沒有幫助它。

我聯系我們表格,當用戶點擊提交按鈕時,我想在我的mailchimp數據庫中添加用戶的電子郵件ID。

我已嘗試使用此代碼,但得到104錯誤..

  $apikey = '***********-us3';
                $listID = '*******';
                $email = "********";
                $url = sprintf('https://us2.api.mailchimp.com/2.0/lists/subscribe&apikey=%s&id=%s&email_address=%s&output=json', $apikey, $listID, $email, $_SERVER['REMOTE_ADDR']);
                $ch = curl_init($url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $data = curl_exec($ch);
                $arr = json_decode($data, true);
                curl_close($ch);
        if ($arr == 1) {
            echo 'Check now your e-mail and confirm your subsciption.';
        } else {
                            echo $arr['code'];
            switch ($arr['code']) {
                case 214:
                echo 'You are already subscribed.';
                break;
                // check the MailChimp API for more options
                default:
                echo 'Unkown error...';
                break;          
            }
        }

任何人都可以建議我如何實現它?

提前致謝

mailchimp在https://bitbucket.org/mailchimp/mailchimp-api-php上提供了一個PHP包裝器,它可以讓生活變得容易一百萬次。

這里似乎發生的問題是你正在進行GET而不是POST

嘗試

$apikey = '**********-us3';
$listID = '******';
$email  = "**************";
$fields = array('apikey' => urlencode($apikey), 'id' => urlencode($listID), 'email_address' => urlencode($email), 'output' => 'json' );
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$url = 'https://us2.api.mailchimp.com/2.0/lists/subscribe';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$data = curl_exec($ch);
$arr = json_decode($data, true);
curl_close($ch);
if ($arr == 1) {
    echo 'Check now your e-mail and confirm your subsciption.';
} else {
                    echo $arr['code'];
    switch ($arr['code']) {
        case 214:
        echo 'You are already subscribed.';
        break;
        // check the MailChimp API for more options
        default:
        echo 'Unkown error...';
        break;          
    }
}

如果有幫助,請告訴我。 如果沒有,我可以看看我是否可以嘗試自己設置,以便我可以測試一些代碼。

還有另一個流行的簡單API https://github.com/drewm/mailchimp-api

use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp('abc123abc123abc123abc123abc123-us1');

$list_id = 'b1234346';
$result = $MailChimp->post("lists/$list_id/members", [
                'email_address' => 'davy@example.com',
                'status'        => 'subscribed',
            ]);

print_r($result);

暫無
暫無

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

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