2017-01-25 12 views
2

PHPのMailChimpリストにメールを登録しようとしています。私が実際にバックエンドの開発者いませんでしたので、私はこの中で停滞してる:(Mailchimp API v3.0を使用して登録

私はMailChimpヘルパーPHPライブラリを使用しています:https://github.com/drewm/mailchimp-api

私はすでに、すべてのインターネットを検索したと私は得ることができるすべての500内部サーバーエラーの状態です。私は本番サーバーですでにだ。

<?php 

include("./inc/MailChimp.php"); 
use \DrewM\MailChimp\MailChimp; 

$api_key = "xxxxxxxxxxx-us13"; 
$list_id = "7xxxxxxx4"; 

$MailChimp = new MailChimp($api_key); 

$result = $MailChimp->post("lists/$list_id/members", [ 
    "email_address" => $_POST["txt_mail"], 
    'merge_fields' => ['FNAME'=>$_POST["txt_name"], 'FPHONE'=>$_POST["txt_phone"], 'FMSG'=>$_POST["txt_message"]], 
    "status"  => "subscribed" 
]); 

if ($MailChimp->success()) { 
    echo "<h4>Thank you, you have been added to our mailing list.</h4>"; 
} else { 
    echo $MailChimp->getLastError(); 
} ?> 
+1

これは12月に廃止されたので、今は3.0を使用するには 'CURL'を使用する必要があります。私の答えを見てください:) – scoopzilla

+2

サーバログを見て '500'エラーの原因を調べる必要があります。私の推測では、PHPのバージョンが5.4より小さく、配列の構文が必要です'[]から' array() 'に固定 – cmorrissey

答えて

2

男ああ、私はそれに遭遇したとき、この問題は私を作った方法挫折見当がつかない。

幸いにも、私はこれを見つけました便利なものMisha Rudrastyh API 3.0には驚くほどよく似ています。ここでは要点は、しかしです:

私はWordpressのを使用していたので、私が最初に私のfunctions.phpファイルに以下のコードを配置(ここではそれはあなたの変数を編集している)

<?php 
    function rudr_mailchimp_subscriber_status($email, $status, $list_id, $api_key, $merge_fields = array('FNAME'=> '', 'FPHONE'=> '', 'FMSG'=> '')){ 
    $data = array(
     'apikey'  => $api_key, 
     'email_address' => $txt_mail, 
     'status'  => $status, 
     'merge_fields' => $merge_fields 
    ); 
$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; 
    } 

THEN私は内の変数のためのフィールドを追加しました私のフォームプロセス:

<?php 
    $email = $_POST['txt_mail']; 
    $FNAME=$_POST['txt_name']; 
    $FPHONE=$_POST['txt_phone']; 
    $FMSG=$_POST['txt_message']; 
    $status = 'pending'; // "subscribed" or "unsubscribed" or "cleaned" or "pending" 
    $list_id = 'xxxxxxxxxxx-us13'; // where to get it read above 
    $api_key = 'xxxxxxxxxxx-us13'; // where to get it read above 
    $merge_fields = array('FNAME' => $FNAME, 'FPHONE' => $FPHONE, 'FMSG' => $FMSG); 

    rudr_mailchimp_subscriber_status($email, $status, $list_id, $api_key, $merge_fields);    

?> 

私はこれが役に立ちます。私はこれをしっかりとやる方法を理解するまでしばらく苦労しました。

+1

男、ありがとうございました!これはまさに私が必要としていたものです。遅い回答をおかけして申し訳ありません、私は私のプロジェクトを仕上げていました。すべての助けをくれた皆さん、ありがとうございました。 –

+0

@Fabio Sampaioあなたは大歓迎です。 – scoopzilla

関連する問題