我終於解決了這個簡單的代碼,沒有其他的庫需要!
<?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);
?>
你真的得到了什麼錯誤? –
打開php的錯誤報告。 https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display –
無法加載資源:服務器迴應狀態爲500() – Filip