繁体   English   中英

使用不带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); 
    }

您需要设置基本身份验证标头。 同样,您似乎也在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