2017-10-06 150 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错误,而不是我们可以使用的东西。几年前我们就已经有了这个综合体,然后它突然间崩溃了。我严重怀疑一些流氓程序员刚进去并改变了这个属性。 –