使用不带 cURL 的 MailChimp API
using MailChimp API without cURL
我正在尝试使用 MailChimp API 在订阅者选中表单上的复选框后将他们添加到列表中。我可以用 cURL 做到这一点,它工作正常,但它所在的服务器没有启用它。我正在尝试使用 file_get_contents(),但这给了我一个 401 错误。我检查了 API 键和列表 ID,它们都是正确的。我猜是我的语法搞砸了,或者我放错了地方,但我没有看到问题所在。我试过的是下面。有人有什么想法吗?
if (isset($_POST['mailtest']) == 'value1'){
$email = $_POST['email'];
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$mailchimp_api_key = 'apikey';
$mailchimp_list_id = 'listid';
$data = array(
'apikey' => $mailchimp_api_key,
'id' => $mailchimp_list_id,
'email' => $email
'status'=>'subscribed'
);
$memberId = md5(strtolower($email));
$dataCenter = substr($mailchimp_api_key ,strpos($mailchimp_api_key, '-')+1);
$endpoint = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $mailchimp_list_id . '/members/' . $memberId;
if ($fname && $lname) $data['merge_vars '] = array('FNAME' => $fname, 'LNAME' => $lname);
$json_data = json_encode($data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/json",
"Connection: close\r\n" .
"Content-length: " . strlen($json_data) . "\r\n",
'data' => $json_data,
),);
$context = stream_context_create($options);
$result = file_get_contents($endpoint, false, $context);
}
您需要设置基本身份验证header。同样,您似乎将数据中心包含在 api 键中,因此下面的代码可能需要进行一些调整,因为我实际上并不知道 $mailchimp_api_key
设置为什么。
// this is improper usage of isset. isset returns a boolean
// since you're not doing a strict match it'll always work,
// unless mailtest is null or isn't set.
if (isset($_POST['mailtest']) == 'value1') {
$email = $_POST['email'];
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$mailchimp_api_key = 'apikey';
$mailchimp_list_id = 'listid';
$data = array(
'apikey' => $mailchimp_api_key,
'id' => $mailchimp_list_id,
'email' => $email
'status'=>'subscribed'
);
$memberId = md5(strtolower($email));
$dataCenter = substr($mailchimp_api_key ,strpos($mailchimp_api_key, '-')+1);
$endpoint = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $mailchimp_list_id . '/members/' . $memberId;
if ($fname && $lname) {
$data['merge_vars '] = array('FNAME' => $fname, 'LNAME' => $lname);
}
$json_data = json_encode($data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => implode(
"\r\n",
[
"Content-type: application/json",
"Content-length: " . strlen($json_data),
"Authorization: Basic " . base64_encode("anystring:$mailchimp_api_key") // see note above
]
),
'content' => $json_data,
)
);
$context = stream_context_create($options);
$result = file_get_contents($endpoint, false, $context);
}
我正在尝试使用 MailChimp API 在订阅者选中表单上的复选框后将他们添加到列表中。我可以用 cURL 做到这一点,它工作正常,但它所在的服务器没有启用它。我正在尝试使用 file_get_contents(),但这给了我一个 401 错误。我检查了 API 键和列表 ID,它们都是正确的。我猜是我的语法搞砸了,或者我放错了地方,但我没有看到问题所在。我试过的是下面。有人有什么想法吗?
if (isset($_POST['mailtest']) == 'value1'){
$email = $_POST['email'];
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$mailchimp_api_key = 'apikey';
$mailchimp_list_id = 'listid';
$data = array(
'apikey' => $mailchimp_api_key,
'id' => $mailchimp_list_id,
'email' => $email
'status'=>'subscribed'
);
$memberId = md5(strtolower($email));
$dataCenter = substr($mailchimp_api_key ,strpos($mailchimp_api_key, '-')+1);
$endpoint = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $mailchimp_list_id . '/members/' . $memberId;
if ($fname && $lname) $data['merge_vars '] = array('FNAME' => $fname, 'LNAME' => $lname);
$json_data = json_encode($data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/json",
"Connection: close\r\n" .
"Content-length: " . strlen($json_data) . "\r\n",
'data' => $json_data,
),);
$context = stream_context_create($options);
$result = file_get_contents($endpoint, false, $context);
}
您需要设置基本身份验证header。同样,您似乎将数据中心包含在 api 键中,因此下面的代码可能需要进行一些调整,因为我实际上并不知道 $mailchimp_api_key
设置为什么。
// this is improper usage of isset. isset returns a boolean
// since you're not doing a strict match it'll always work,
// unless mailtest is null or isn't set.
if (isset($_POST['mailtest']) == 'value1') {
$email = $_POST['email'];
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$mailchimp_api_key = 'apikey';
$mailchimp_list_id = 'listid';
$data = array(
'apikey' => $mailchimp_api_key,
'id' => $mailchimp_list_id,
'email' => $email
'status'=>'subscribed'
);
$memberId = md5(strtolower($email));
$dataCenter = substr($mailchimp_api_key ,strpos($mailchimp_api_key, '-')+1);
$endpoint = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $mailchimp_list_id . '/members/' . $memberId;
if ($fname && $lname) {
$data['merge_vars '] = array('FNAME' => $fname, 'LNAME' => $lname);
}
$json_data = json_encode($data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => implode(
"\r\n",
[
"Content-type: application/json",
"Content-length: " . strlen($json_data),
"Authorization: Basic " . base64_encode("anystring:$mailchimp_api_key") // see note above
]
),
'content' => $json_data,
)
);
$context = stream_context_create($options);
$result = file_get_contents($endpoint, false, $context);
}