Senne Vandenputte
Senne Vandenputte

Reputation: 539

PHP - How to subscribe user to MailerLite using API

On my website, people can register to become a member. Now I want them to be also immediately be subscribed to my MailerLite mailinglist. In the API documentation I have fount the following PHP example which should work:

$ML_Subscribers = new MailerLite\Subscribers('xxxxxxxxxxxxxxxxxxxxxx');
            $subscriber = array(
                'email' => $e,
                'name' => $f,
            );
            $subscriber = $ML_Subscribers->setId('xxxxxxxxxxx')->setAutoresponders(false)->add($subscriber);

The API url that's mentioned on their website is the following:

https://app.mailerlite.com/api/v1/subscribers/{list_id}/

I am unsure how to implement this URL in the script.. I have the above PHP code that should add a subscriber to the list, but should I also include the link in some way? I could use some help to make it work. Thanks in advance!

Upvotes: 0

Views: 4444

Answers (1)

Ohgodwhy
Ohgodwhy

Reputation: 50787

When using the MailerLite SDK it is not required to post to any specific end point. There are wrappers within the functions that dictate the endpoint and method request type when sending data to their API.

The provided code below is all that is required:

$ML_Subscribers = new MailerLite\Subscribers( API_KEY );

$subscriber = array(
    'email' => $e,
    'name' => $f,
);

$subscriber = $ML_Subscribers->setId( LIST_ID )->setAutoresponders(false)->add( $subscriber );

Where you would replace API_KEY with the provided MailerLite API key, and LIST_ID with the ID of the list that you wish to add the subscriber to.

Otherwise if you weren't using their SDK you would need to make a POST to their endpoint: POST https://app.mailerlite.com/api/v1/subscribers/{list_id}/. You would also be required to construct the proper data object to be sent over containing your API Key, an Email and the id of the subscriber list.

You can read further about this in the MailerLite Documentation

Upvotes: 2

Related Questions