2017-10-06 109 views
0

我正在嘗試使PHP代碼取消訂閱/訂閱給定電子郵件的用戶。Mailchimp API PHP:500錯誤

我發現這個教程: http://www.sutanaryan.com/2016/10/mailchimp-api-subscribe-or-unsubscribe-user-php-script/

但我仍然得到錯誤。我認爲有此文件中的一個問題:

要求( 'mailchimp/Mailchimp.php');

在錯誤日誌中什麼也沒有。

任何人可以給我建議如何排除故障或以不同的方式解決它?我幾乎沒有關於PHP的知識。

謝謝

+0

你真的得到了什麼錯誤? –

+0

打開php的錯誤報告。 https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display –

+0

無法加載資源:服務器迴應狀態爲500() – Filip

回答

1

我終於解決了這個簡單的代碼,沒有其他的庫需要!

<?php 

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

$email = 'XXXXXXXXXXXXXXXX'; 
$status = 'subscribed'; // "subscribed" or "unsubscribed" or "cleaned" or "pending" 
$list_id = 'XXXXXXXXXX'; // where to get it read above 
$api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'; // where to get it read above 
$merge_fields = array('FNAME' => 'Misha','LNAME' => 'Rudrastyh'); 

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


?> 
+0

嘿,如果你不清楚你的方法爲什麼會起作用,那麼「merge_fields」鍵看起來像它曾經是「merge_vars」,這對我造成了500錯誤......我無法證明任何事情,但我說懷疑MC在某些時候發生了變化,他們只是決定返回一個無用的500錯誤,而不是我們可以使用的東西。幾年前我們就已經有了這個綜合體,然後它突然間崩潰了。我嚴重懷疑一些流氓程序員剛進去並改變了這個屬性。 –