通过MailChimp API创建Campaign

时间:2017-11-24 23:00:19

标签: mailchimp-api-v3.0

我想通过MailChimp API创建广告系列。我正在使用下面的代码。

        $data = ["recipients" => ["list_id" => "a332819b0b"], "type" => "regular", "settings" => ["subject_line" => "Subject", "title" => "Title", "reply_to" => "test@gmail.com", "from_name" => "Test"]];
        $data = json_encode($data);
        $curl = curl_init();
        curl_setopt_array($curl, [
            //Sample url
            CURLOPT_URL => "https://us17.api.mailchimp.com/3.0/campaigns",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $data,
            CURLOPT_HTTPHEADER => ["apikey:MY_API_KEY"],
        ]);

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
            $response = $err;
        }

        die($response);

我得到的输出如下。

enter image description here

有人能帮我解决这个问题吗?感谢。

1 个答案:

答案 0 :(得分:0)

你几乎已经开始工作了。只需将CURLOPT_HTTPHEADER行替换为CURLOPT_USERPWD,如下所示:

CURLOPT_USERPWD => "apikey:abc123abc123abc123abc123abc123-us10"

当然,您希望在那里使用自己的API密钥,它应该可以正常工作。