Skip to content

Instantly share code, notes, and snippets.

@erfg12
Last active January 23, 2021 16:33
Show Gist options
  • Save erfg12/37070ad04d849b246e4bdd15d5d6646f to your computer and use it in GitHub Desktop.
Save erfg12/37070ad04d849b246e4bdd15d5d6646f to your computer and use it in GitHub Desktop.
Patreon API PHP Example
<?php
require __DIR__ . '/vendor/autoload.php';
use Patreon\API;
use Patreon\OAuth;
// get client_id, client_secret, redirect_uri and creator_token from https://www.patreon.com/portal/registration/register-clients
$client_id = "";
$client_secret = "";
$redirect_uri = "";
$creator_token = "";
// [USER] login and show token. 0 = failed
function login() {
global $client_id, $client_secret, $redirect_uri;
// no saved token cookies or codes from Patreon redirect, get a new one
if (!isset($_GET['code'])){
echo 'Redirecting to Patreon auth site... ';
$loginURL = "https://www.patreon.com/oauth2/authorize?response_type=code&client_id=$client_id&redirect_uri=$redirect_uri";
?><script>window.location = "<?PHP echo $loginURL; ?>";</script><?PHP
} else { // we have a token cookie or redirect code
if (isset($_GET['code'])) { // found redirect code, save to cookie and send to final destination.
$oauth_client = new Patreon\OAuth($client_id, $client_secret);
$tokens = $oauth_client->get_tokens($_GET['code'], $redirect_uri);
if (array_key_exists('access_token', $tokens))
return $tokens['access_token'];
else
return 0;
} else
return 0;
}
}
// [USER] get Patreon user ID from token. Good for storing in DB to check later.
function getUserID($token) {
$api_client = new API($token);
$patron_response = $api_client->fetch_user();
$user = $patron_response->get('data');
return $user->get('id');
}
// [CREATOR] check a users patronage by the user id and campaign id. These should be stored in a DB somewhere to check on the fly.
function checkPatronage($userID, $campaignID) {
global $creator_token;
$api_client = new API($creator_token);
$campaigns_response = $api_client->fetch_page_of_pledges($campaignID, 50000);
$campaign = $campaigns_response->get('data');
$userinfo = $campaigns_response->get('included');
for ($i = 0; $i <= 50000; $i++) {
if (!$userinfo->has($i) || !$campaign->has($i) || !$userinfo->get($i)->has('attributes.full_name'))
continue;
if ($userinfo->get($i)->get('id') == $userID)
return $campaign->get($i)->attribute('amount_cents');
}
return 0;
}
// [CREATOR] print a list of client IDs and patronage amount (go to campaign page, view source, search for "campaign")
function CampaignPatronsList($campaignID) {
global $creator_token;
$api_client = new API($creator_token);
$campaigns_response = $api_client->fetch_page_of_pledges($campaignID, 50000);
$campaign = $campaigns_response->get('data');
$userinfo = $campaigns_response->get('included');
$c = 0;
$total = 0;
for ($i = 0; $i <= 50000; $i++) {
if (!$userinfo->has($i) || !$campaign->has($i) || !$userinfo->get($i)->has('attributes.full_name'))
continue;
echo $userinfo->get($i)->get('id').' | '.$userinfo->get($i)->attribute('full_name').': '.$campaign->get($i)->attribute('amount_cents').'<br>';
$c++;
$total += $campaign->get($i)->attribute('amount_cents');
}
$total = number_format($total, 2);
echo "<p>Patron Quantity: $c, Total Money: $total</p>";
}
?>
@goulart81
Copy link

There is a continue inside the for loops.
Shouldn't it be break instead of continue ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment