Search code examples
phpmailchimpmailchimp-api-v3.0

How to get unsubscribe URL from Mailchimp list ID API


I'm struggling to get the unsubscribe URL from the Mailchimp API. I've managed to find the subscribe URL by accessing get("lists/{$list_id}/signup-forms"); which returns the following json. However, there's no unsubscribe URL displaying and I can't think of other actions to call apart from signup-forms.

When checking the unsubscribe link in the mailchimp account manually, both the subscribe and unsubscribe links look identical apart from the /subscribe. So my next approach would be to get the subscribe_url_long and replace the string subscribe with unsubscribe.

Unless anyone knows a way of accessing the the property from the list ID?

    {
   "subscribe_url_long": "https://live.us3.list-manage.com/subscribe?u=XXXXX&id=XXXXX",
    }

Solution

  • If anyone wonders how I fixed this, I managed to grab the following code from the API using the code blow:

    public
        function get_unsubscribe_url(string $list_id, array $fields = []) {
            $list = $this->get("lists/{$list_id}");
            $subscribe_url = $list['subscribe_url_long'];
            $unsubsribe_link = str_replace('subscribe', 'unsubscribe', $subscribe_url);
            return $unsubsribe_link;
        }