简体   繁体   English

使用不带cURL的MailChimp API

[英]using MailChimp API without cURL

I'm trying to use the MailChimp API to add subscribers to a list once they've checked a box on a form. 我正在尝试使用MailChimp API将订阅者添加到列表,一旦他们选中了表单上的复选框。 I can do this with cURL, and it works fine, but the server that this will be on does not have it enabled. 我可以使用cURL做到这一点,并且工作正常,但是将要启用的服务器未启用它。 I'm trying to use file_get_contents(), but that gives me a 401 error. 我正在尝试使用file_get_contents(),但这给了我401错误。 I've checked the API key and the list id, and they're both correct. 我检查了API密钥和列表ID,它们都是正确的。 I'm guessing that my syntax is messed up or that I've put something in the wrong place, but I'm not seeing the problem. 我猜想我的语法搞砸了,或者我在错误的地方放了东西,但是我没有看到问题。 What I've tried is below. 我尝试过的如下。 Anyone have any ideas? 有人有想法么?

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); 
    }

You need to set the Basic authentication header. 您需要设置基本身份验证标头。 As well it seems you're including your dataCentre in with your api key, so the below code might need some tweaking since I don't actually know what $mailchimp_api_key is set to. 同样,您似乎也在api键中包含了dataCentre,因此以下代码可能需要进行一些调整,因为我实际上并不知道$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); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM