简体   繁体   中英

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. I can do this with cURL, and it works fine, but the server that this will be on does not have it enabled. I'm trying to use file_get_contents(), but that gives me a 401 error. I've checked the API key and the list id, and they're both correct. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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