Using Patreon’s API with PHP

Examining the Patreon API and how to use it in PHP.

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

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.

$access_token = 'YOUR_ACCESS_TOKEN';
$api_client = new \Patreon\API($access_token);

// Retrieve our campaigns
$campaign_response = $api_client->fetch_campaigns();

foreach($campaign_response['data'] as $campaign) {
    // Fetch the campaign details
    $campaign_details = $api_client->fetch_campaign_details($campaign['id']]);
    
    // Fetch campaign members
    $members = $api_client->fetch_page_of_members_from_campaign($campaign['id'], 200);
    foreach($members['data'] as $member) {
        // Fetch member details
        $member_details = $api_client->fetch_member_details($member['id']);

        // The member's relationship with the current campaign
        $member_relationships = $member_details['data']['relationships'];

        // The member's current tiers (array).
        $member_tiers = $member_relationships['currently_entitled_tiers']['data'];
    }
}

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.

$client_id = 'YOUR_CLIENT_ID';
$client_secret = 'YOUR_CLIENT_SECRET';

$refresh_token = 'YOUR_INITIAL_REFRESH_TOKEN';

$oauth_client = new \Patreon\OAuth($client_id, $client_secret);

// Refresh tokens using Patreon's OAuth client.
$tokens = $oauth_client->refresh_token($refresh_token);

if($tokens['access_token']) {
    $new_access_token = $tokens['access_token'];
    $new_refresh_token = $tokens['refresh_token'];

    // Save the new tokens to the database
    $db->insert('INSERT INTO patreon_tokens (id, access_token, refresh_token, creation_date) 
                 VALUES (NULL, ?, ?, ?)',
                 [
                     $new_access_token,
                     $new_refresh_token,
                     time()
                 ]);
} else {
    echo 'Uh-oh! Something bad happened:' . PHP_EOL;
    echo json_encode($tokens, JSON_PRETTY_PRINT);
}

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.

/* Example code that stores Patreon tokens on a database. */

// Fetches the latest entry from the patreon_tokens database.
$query = $db->query('SELECT * FROM patreon_tokens ORDER BY id DESC LIMIT 1');

// If the token is over 28 days old then refresh it.
if($query->creation_date + 2419200 < time()) {
    $new_tokens = $oauth_client->refresh_token($query->refresh_token, null);

    if(!$new_tokens['access_token']) {
        echo 'Uh-oh! Something bad happened: ' . PHP_EOL;
        echo json_encode($tokens, JSON_PRETTY_PRINT);
        exit;
    }

    $new_access_token = $new_tokens['access_token'];
    $new_refresh_token = $new_tokens['refresh_token'];

    $db->insert('INSERT INTO patreon_tokens (id, access_token, refresh_token, creation_date) 
                 VALUES (NULL, ?, ?, ?)',
                 [
                     $new_access_token,
                     $new_refresh_token,
                     time()
                 ]);

    $access_token = $new_access_token;
} else {
    // Assume that the access token is still valid.
    $access_token = $query->access_token;
}

$api_client = new \Patreon\API($access_token);

// Do your thing here

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.