Lugifah
Lugifah

Reputation: 79

Quick q. Mailchimp API 3.0

Can i use the first script or i need to use the curl option for mailchimp 3.0? I read some posts that the first one may be depreciated.. is that correct? Note that i'm not running on WordPress. Thank you for your fast answers.

        <?php
    require("vendor/autoload.php");

    use \DrewM\MailChimp\MailChimp;
    $mc = new MailChimp('apikey');

    $email = $_POST['email'];
    $subscriber_hash = $mc->subscriberHash($email);

    $response = [];
    $list_id = 'listid';

    $resp = $mc->get("/lists/$list_id/members/$subscriber_hash";

    if ($mc->success()) {
        $response['message'] = 'Thank you for subscribing to the mailing list';

        // User successfully subscribed - set HTTP status code to 200
        http_response_code(200);
    } else {
        $response['message'] = $mc->getLastError();

        // User not subscribed - set HTTP status code to 400
        http_response_code(400);
    }

    // Return json-formatted response
    echo json_encode($response);
    ?>

Or should i use this one?

        function mc_checklist($email, $debug, $apikey, $listid, $server) {
        $userid = md5($email);
        $auth = base64_encode( 'user:'. $apikey );
        $data = array(
            'apikey'        => $apikey,
            'email_address' => $email
            );
        $json_data = json_encode($data);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'.api.mailchimp.com/3.0/lists/'.$listid.'/members/' . $userid);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
            'Authorization: Basic '. $auth));
        curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
        $result = curl_exec($ch);
        if ($debug) {
            var_dump($result);
        }
        $json = json_decode($result);
        echo $json->{'status'};
    }

This row is only for stackoverflow not alowing me to post that much code without including more details.

Upvotes: 0

Views: 219

Answers (1)

Joel H.
Joel H.

Reputation: 690

The first is not deprecated. It uses this wrapper to make API calls using the same API version that the second block of code uses. It's just simpler to work with so you don't have to write separate CURL requests for every call. You can also take a look at some of its source code and notice that it uses CURL anyway to make its calls.

So either way will use CURL, and whichever option you choose is simply a matter of preference.

Hopefully that clears it up for you!

Upvotes: 1

Related Questions