我试图使用MailChimp API将订阅者添加到列表中,一旦他们检查了窗体上的框。我可以用cURL来做到这一点,并且它可以正常工作,但是这个服务器不会启用它。我试图使用file_get_contents(),但是这给了我一个401错误。我检查了API密钥和列表ID,它们都是正确的。我猜测我的语法已经搞乱了,或者我把错误的地方放在了一些地方,但是我没有看到这个问题。我试过的是以下。有人有主意吗?使用MailChimp API没有cURL
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);
}
http 401错误意味着未经授权。所以你没有正确的认证。 – Augwa
如果你想坚持'file_get_contents()',你需要一些上下文来验证(阅读手册)。看看[stream_context_create()](http://php.net/manual/en/function.stream-context-create.php)来创建你需要的头文件。 – fpietka