如何从 MailChimp 3.0 API 获取兴趣小组?

How can I get interest groups from the MailChimp 3.0 API?

有人知道在 Mailchimp 的 3.0 API 中获取所有兴趣组列表的正确端点是什么吗?我能找到的唯一文档在这里:https://github.com/mailchimp/APIv3-examples/wiki/Resources

当我尝试使用 /3.0/lists/{list_id}/interest-groupings 时,我得到以下响应:

Class Object ( [type] => http://kb.mailchimp.com/api/error-docs/404-resource-not-found [title] => Resource Not Found [status] => 404 [detail] => The resource 'InterestGroupings_Collection' could not be found. [instance] => )

请求的 url 看起来像这样:https://DATA_CENTER.api.mailchimp.com/3.0/lists/LIST_ID/interest-groupings?apikey=API_KEY

我显然没有正确的端点,但我想我会问,因为我找到了我的其他问题的答案

您正在查看测试版中严重过时的文档——您最好查看 API Playground,但您要查找的端点是 interest-categories

虽然这是一个老问题,但我发布这个答案是因为所选答案不再有效。建议的 'API Playground' 已被 MailChimp 停用。

我正在使用简单的 CURL 命令。您可以使用您正在使用的语言实现 CURL。 请注意,CURL 命令中的 ${dc} 将替换为您的 MailChimp 服务器前缀。

  1. 为此,您首先需要 fetch/check the id of the list:

    curl -X GET \ 'https://${dc}.api.mailchimp.com/3.0/lists?fields=<SOME_ARRAY_VALUE>&exclude_fields=<SOME_ARRAY_VALUE>&count=10&offset=0&before_date_created=<SOME_STRING_VALUE>&since_date_created=<SOME_STRING_VALUE>&before_campaign_last_sent=<SOME_STRING_VALUE>&since_campaign_last_sent=<SOME_STRING_VALUE>&email=<SOME_STRING_VALUE>&sort_field=<SOME_STRING_VALUE>&sort_dir=<SOME_STRING_VALUE>&has_ecommerce_store=<SOME_BOOLEAN_VALUE>' \ --user "anystring:${apikey}"'

  2. 从上面的输出中得到指定的 ID ({list_id}) 后,fetch the interest categories:

    curl -X GET \ 'https://${dc}.api.mailchimp.com/3.0/lists/{list_id}/interest-categories?fields=<SOME_ARRAY_VALUE>&exclude_fields=<SOME_ARRAY_VALUE>&count=10&offset=0&type=<SOME_STRING_VALUE>' \ --user "anystring:${apikey}"'

  3. 注意上面输出中的 {interest_category_id}。最后到get the interest details,使用:

    curl -X GET \ 'https://${dc}.api.mailchimp.com/3.0/lists/{list_id}/interest-categories/{interest_category_id}/interests?fields=<SOME_ARRAY_VALUE>&exclude_fields=<SOME_ARRAY_VALUE>&count=10&offset=0' \ --user "anystring:${apikey}"'

示例 PHP 函数(仅适用于步骤 3)如下:

function mailchimp_findgrpids()
    {
        $mcurl = 'https://'.MAILCHIMP_SERVER_PREFIX.'.api.mailchimp.com/3.0/lists/'.MAILCHIMP_LIST.'/interest-categories/'.MAILCHIMP_INTEREST_CAT.'/interests';
        
        $ch = curl_init($mcurl);

    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . MAILCHIMP_API_KEY);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $result = curl_exec($ch);
    echo $result;
    curl_close($ch);
    }