Introduction
Patreon is a membership platform that provides tools for content creators to run a subscription service.
In this article, you will examine how to use the Patreon API in your PHP project. The Patreon API allows you to fetch data like benefits, profile info, pledges, tiers and much more from your Patreon creator profile.
Prerequisites
- An understanding of PHP is required.
- A registered API client on Patreon and it’s tokens
- patreon-php
Initializing the API client and Fetching Data
You can initialize the API client with your client’s access token. Keep in mind that an access token gets refreshed every a month and when an OAuth authentication is made.
This code retrieves a list of campaigns from the Patreon API, then iterates through each one to fetch campaign details, member details, and member relationships. For each member, the code also retrieves the member’s current tiers.
Refreshing and Saving the Access Token
You can retrieve new tokens via OAuth. Retrieving new tokens will invalidate your client’s current access and refresh token.
This code sets up a refresh token using the Patreon OAuth client. It then refreshes the token and saves the new ones to the database.
You can create a cron job that will run a script that updates your tokens and saves them to a database.
In this code, we are storing Patreon tokens inside the database. It’s also checking to see if the token is over 28 days old and if it is, then it refreshes the token.