You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Patreon PHP Library 1.0.0 - using API v2 - Rebased from Dev fork to Patreon master (#61)
* Art4 JSON lib removed
* Art4 removed from dependency list
* Autoloader to PSR4. API classes one level up in dir structure. To be used in dev package in packagist
* Vendor name update
* Vendor name update 2
* PSR4 namespace update
* Readme updated with a warning
* Namespace corrections
* Updated the package info with warning
* Namespaces updated for test package
* Composer file update.
* Namespace test
* Namespace test 2
* Namespace test 3
* Readme updated. Now unneeded patreon.php file removed
* Dev keys removed from readme
* Modified the get_data function to allow array, object or raw JSON returns. Renamed __get_data to get_data. get_data function made public so that it is usable directly
* Changed default value of and added a catch all.
* Changed return_format handling to object var. Set default value. Changed functions to use this var
* Updated the example in readme
* Caching var added to hold the cURL handler. cURL handler function set to return cached handler to bump up performance
* Now deprecated parse parameter removed from API functions
* Reverted curl handler caching since theres no efficient way to separate the suffix. Caching will be used on a per-call basis.
* Reverted recent changes to a state without Art4 lib and with psr4 autoloading for intermediate PR
* Caching logic added to cache API requests to avoid repeated calls for same request in the same page run. api_endpoint var added to set the API endpoint. API.php cleanup and styling changes.
* Made fetch_user use v2 endpoint. Scopes added. Example updated to use scopes in login link. Problematic curl header array corrected - now the bearer token is passed properly. Works and gets v2 return for fetch_user from the API
* Removed debug header return code
* oAuth login example added with details. Example for properly getting user details and membership info added. Readme updated to stress importance of scopes. Scope requesting added to Readme example
* All calls translated to V2
* Added api_request_method (default GET) and curl_postfields variables to enable different types of cURL requests to API and to carry the payload for operations other than GET. Set the default request method to GET
* Using the new format option added to create_ch, added an example that creates a webhook by using POST method with POSTFIELDS payload (JSON object)
* Added an example using the universal/unified flow API endpoint (/become-patron) for an example that allows unified register/login/pledge/unlock flow
* Typo corrections
* Added login and unlock button images to assets
* Updated readme example to test
* Corrections, styling
* Namespace set to Patreon, debug code removed
* composer.json changed to reflect Patreon master
* Minor corrections, namespace changed to Patreon, minor styling
* Corrections, styling, more info for scopes in readme and examples
* Readme correction, styling
* Namespaces and composer json info changed for live packagist test
* Minor corrections. Unnecessary patron_list.php removed. Namespaces re-set to Patreon. composer.json updated to reflect Patreon master repo
* Changelog updated
* Changelog updated 2
* Minor typos and corrections in examples. All examples re-tested
* Art4 JSON lib removed
* Art4 removed from dependency list
* Autoloader to PSR4. API classes one level up in dir structure. To be used in dev package in packagist
* Vendor name update
* Vendor name update 2
* PSR4 namespace update
* Readme updated with a warning
* Namespace corrections
* Updated the package info with warning
* Namespaces updated for test package
* Composer file update.
* Namespace test
* Namespace test 2
* Namespace test 3
* Readme updated. Now unneeded patreon.php file removed
* Dev keys removed from readme
* Modified the get_data function to allow array, object or raw JSON returns. Renamed __get_data to get_data. get_data function made public so that it is usable directly
* Changed default value of and added a catch all.
* Changed return_format handling to object var. Set default value. Changed functions to use this var
* Updated the example in readme
* Caching var added to hold the cURL handler. cURL handler function set to return cached handler to bump up performance
* Now deprecated parse parameter removed from API functions
* Reverted curl handler caching since theres no efficient way to separate the suffix. Caching will be used on a per-call basis.
* Reverted recent changes to a state without Art4 lib and with psr4 autoloading for intermediate PR
* Caching logic added to cache API requests to avoid repeated calls for same request in the same page run. api_endpoint var added to set the API endpoint. API.php cleanup and styling changes.
* Made fetch_user use v2 endpoint. Scopes added. Example updated to use scopes in login link. Problematic curl header array corrected - now the bearer token is passed properly. Works and gets v2 return for fetch_user from the API
* Removed debug header return code
* All calls translated to V2
* Updated readme example to test
* Corrections, styling
* Added api_request_method (default GET) and curl_postfields variables to enable different types of cURL requests to API and to carry the payload for operations other than GET. Set the default request method to GET
* Namespace set to Patreon, debug code removed
* composer.json changed to reflect Patreon master
* oAuth login example added with details. Example for properly getting user details and membership info added. Readme updated to stress importance of scopes. Scope requesting added to Readme example
* Using the new format option added to create_ch, added an example that creates a webhook by using POST method with POSTFIELDS payload (JSON object)
* Added an example using the universal/unified flow API endpoint (/become-patron) for an example that allows unified register/login/pledge/unlock flow
* Typo corrections
* Added login and unlock button images to assets
* Minor corrections, namespace changed to Patreon, minor styling
* Corrections, styling, more info for scopes in readme and examples
* Readme correction, styling
* Namespaces and composer json info changed for live packagist test
* Minor corrections. Unnecessary patron_list.php removed. Namespaces re-set to Patreon. composer.json updated to reflect Patreon master repo
* Changelog updated
* Changelog updated 2
* Minor typos and corrections in examples. All examples re-tested
* Added API v2 update, 1.0.0 release compatibility notice. Removed dev version notification
* Minor variable name error in unified flow example corrected
* Readme text updated. Copyright corrected to 2019 from 2018 in notice.md
Copy file name to clipboardExpand all lines: CHANGELOG.md
+16Lines changed: 16 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,19 @@
1
+
# 1.0.0
2
+
3
+
* Library moved to use Patreon API v2 endpoints and calls
4
+
* Scopes added to examples and API url functions
5
+
* Autoloading has been moved to PSR4
6
+
* Art4 JSON parser library removed
7
+
* API class set to provide returns from the API in JSON, array, or object form - choice to parse in other ways (like using Art4 lib is left to the developer - ie, get JSON return and then feed to Art4 lib which can be installed separately)
8
+
* Caching of unique calls to API implemented in API class in order to speed up any repeated calls and reduce load on the API
9
+
* Library directory structure simplified
10
+
* Unnecessary includes and unnecessary files removed
11
+
* login_with_patreon.png and unlock_with_patreon.png images are added under assets/images for developers to use with their apps
12
+
* Unified flow example added to provide an example for content unlocking by having users login/register/pledge at Patreon and return to the app in just one smooth flow
13
+
* Webhook write example added - creates a webhook to notify your local app when there are any membership changes in your campaign
14
+
* Examples made more detailed and comprehensive
15
+
* Readme example reformatted, updated to API V2
16
+
1
17
# 0.3.2
2
18
3
19
* Art4 JSON lib removed in preparation for API v2 upgrade
## Important notice about updating to 1.0.0 from earlier versions
6
+
7
+
Patreon PHP library version 1.0.0 moves on to Patreon's v2 API, which is not compatible with old v1 calls. It also removes Art4 JSON library. Therefore directly upgrading from older versions to 1.0.0 would break compatibility of your installation. APIv1 will deprecated some time soon after APIv2 come out of beta, so its important to get your integration compatible with API v2. With API v2, you can only get access to the scopes you requested during authorization, and you need to ask for the data you want through includes.
8
+
9
+
If you were using Art4 library before, you can separately install it and feed the return from API calls to Art4 library after getting it as JSON from this library. This will make your existing code that uses Art4 library compatible. However note that you will need to track Art4 library updates and resulting compatibility issues yourself.
10
+
11
+
https://docs.patreon.com/#apiv2-oauth
12
+
13
+
If you have any questions or issues, please visit our developer forum at https://www.patreondevelopers.com/
14
+
4
15
## Installation
5
16
Get the plugin from [Packagist](https://packagist.org/packages/patreon/patreon)
6
17
7
18
## Usage
19
+
8
20
### Step 1. Get your client_id and client_secret
21
+
9
22
Visit the [Patreon platform documentation page](https://www.patreon.com/platform/documentation)
10
23
while logged in as a Patreon creator to register your client.
11
24
12
25
This will provide you with a `client_id` and a `client_secret`.
13
26
14
27
### Step 2. Use this plugin in your code
28
+
15
29
Let's say you wanted to make a "Log In with Patreon" button.
16
-
You've read through [the directions](https://www.patreon.com/platform/documentation/oauth),
17
-
and are trying to implement "Step 2: Handling the OAuth Redirect" with your server.
18
-
The user will be arriving at one of your pages *after* you have sent them to the authorize page (at https://www.patreon.com/oauth2/authorize) for step 1,
19
-
so in their query parameters landing on this page,
20
-
they will have a parameter `'code'`.
30
+
31
+
You've read through [the directions](https://www.patreon.com/platform/documentation/oauth), and are trying to implement "Step 2: Handling the OAuth Redirect" with your server.
32
+
33
+
The user will be arriving at one of your pages *after* you have sent them to [the authorize page] (www.patreon.com/oauth2/authorize) for step 1, so in their query parameters landing on this page, they will have a parameter `'code'`.
21
34
22
35
_(If you are doing something other than the "Log In with Patreon" flow, please see [the examples folder](examples) for more examples)_
23
36
37
+
_(Especially the unified flow is a great way to have users unlock locked features or content at your site or app - it allows users to register, login, pledge and return to your app in one smooth unified flow. Check it out in [the examples folder](examples) )_
38
+
39
+
24
40
```php
25
41
<?php
26
-
27
-
/*
28
-
* Use the following,
29
-
* or if you installed via Composer, it should already be required via autoloader
30
-
*/
42
+
43
+
// This example shows how to have your users log in via Patreon, and acquire access and refresh tokens after logging in
31
44
32
45
require_once __DIR__.'/vendor/autoload.php';
33
46
@@ -37,39 +50,87 @@ use Patreon\OAuth;
37
50
$client_id = ''; // Replace with your data
38
51
$client_secret = ''; // Replace with your data
39
52
40
-
$oauth_client = new OAuth($client_id, $client_secret);
53
+
// Set the redirect url where the user will land after oAuth. That url is where the access code will be sent as a _GET parameter. This may be any url in your app that you can accept and process the access code and login
41
54
42
-
$redirect_uri = ""; // This is where the user should land after returning from Patreon
// You can send an array of vars to Patreon and receive them back as they are. Ie, state vars to set the user state, app state or any other info which should be sent back and forth.
65
+
66
+
$state = array();
67
+
68
+
// For example lets set final page which the user needs to land at - this may be a content the user is unlocking via oauth, or a welcome/thank you page
// Now place the url into a login link. Below is a very simple login link with just text. in assets/images folder, there is a button image made with official Patreon assets (login_with_patreon.png). You can also use this image as the inner html of the <a> tag instead of the text provided here
84
+
85
+
// Scopes! You must request the scopes you need to have the access token.
86
+
// In this case, we are requesting the user's identity (basic user info), user's email
87
+
// For example, if you do not request email scope while logging the user in, later you wont be able to get user's email via /identity endpoint when fetching the user details
88
+
// You can only have access to data identified with the scopes you asked. Read more at https://docs.patreon.com/#scopes
// Simply echoing it here. You can present the login link/button in any other way.
97
+
47
98
echo '<ahref="'.$href.'">Click here to login via Patreon</a>';
48
-
echo '<br>';
99
+
100
+
// Up to this part we handled the way to prepare a login link for users to log in via Patreon oAuth using API v2. From this point on starts the processing of a logged in user or user returning from Patreon oAuth.
101
+
102
+
// The below code snippet needs to be active wherever the the user is landing in $redirect_uri parameter above. It will grab the auth code from Patreon and get the tokens via the oAuth client
// There will be some advice for devs on how to save their tokens and match it to their users here
106
+
$oauth_client = new OAuth($client_id, $client_secret);
57
107
58
-
$api_client = new API($access_token);
59
-
60
-
// Return from the API can be received in either array, object or JSON formats by setting the return format. It defaults to array if not specifically set. Specifically setting return format is not necessary. Below is shown as an example of having the return parsed as an object. If there is anyone using Art4 JSON parser lib or any other parser, they can just set the API return to JSON and then have the return parsed by that parser
// Here, you should save the access and refresh tokens for this user somewhere. Conceptually this is the point either you link an existing user of your app with his/her Patreon account, or, if the user is a new user, create an account for him or her in your app, log him or her in, and then link this new account with the Patreon account. More or less a social login logic applies here.
66
114
67
-
echo '<pre>';
68
-
print_r($patron_response);
69
-
echo '</pre>';
115
+
// Only use user's email address info coming from Patreon if the email is verified. Check for is_email_verified value in user's API return.
70
116
71
117
}
72
118
119
+
// After linking an existing account or a new account with Patreon by saving and matching the tokens for a given user, you can then read the access token (from the database or whatever resource), and then just check if the user is logged into Patreon by using below code. Code from down below can be placed wherever in your app, it doesnt need to be in the redirect_uri at which the Patreon user ends after oAuth. You just need the $access_token for the current user and thats it.
120
+
121
+
// Lets say you read $access_token for current user via db resource, or you just acquired it through oAuth earlier like the above - create a new API client
122
+
123
+
$api_client = new API($access_token);
124
+
125
+
// Return from the API can be received in either array, object or JSON formats by setting the return format. It defaults to array if not specifically set. Specifically setting return format is not necessary. Below is shown as an example of having the return parsed as an object. Default is array (associated) and there is no need to specifically set it if you are going to use it as an array. If there is anyone using Art4 JSON parser lib or any other parser, they can just set the API return to json and then have the return parsed by that parser
126
+
127
+
// You dont need the below line if you are going to use the return as array.
128
+
$api_client->api_return_format = 'object';
129
+
130
+
// Now get the current user:
131
+
132
+
$patron_response = $api_client->fetch_user();
133
+
134
+
// At this point you can do anything with the user return. For example, if there is no return for this user, then you can consider the user not logged into Patreon. Or, if there is return, then you can get the user's Patreon id or pledge info. For example if you are able to acquire user's id, then you can consider the user logged into Patreon.
// This example shows you how to create a webhook at Patreon API to notify you when you have any member changes in your campaign
9
+
10
+
// Create a client first, using your creator's access token
11
+
$api_client = newAPI('YOURCREATORSACCESSTOKEN');
12
+
13
+
// If you dont know the campaign id you are targeting already, fetch your campaigns and get the id for the campaign you need. If you already know your campaign id, just skip this part
// If you have more than one campaign in the return, you have to iterate to find the one you want. If return format is array (as is default), just iterate the array and get the id you want
20
+
21
+
// Now, set the API client's cURL request method to POST because webhooks endpoint requires POST for creating webhooks. No need to do that for other requests since API client defaults to GET. But if you set this for a specific instance of the client to anything other than GET, you need to revert it back to default by re-setting it to GET after you make your POST call
22
+
23
+
$api_client->api_request_method = 'POST';
24
+
25
+
// Now set the POSTFIELDS that will contain the payload in cURL request and create the webhook. This particular webhook notifies your application whenever you get a new member, a member updates his/her membership, or a member is removed
26
+
27
+
$api_client->curl_postfields = array (
28
+
'data' => array (
29
+
'type' => 'webhook',
30
+
'attributes' => array (
31
+
'triggers' => array (
32
+
'members:create',
33
+
'members:update',
34
+
'members:delete',
35
+
),
36
+
'uri' => 'https://pat-php-dev.codebard.com', // Note that your url must start with https://
37
+
),
38
+
'relationships' => array (
39
+
'campaign' => array (
40
+
'data' => array (
41
+
'type' => 'campaign',
42
+
'id' => $campaign_id, // Notice how your campaign id has to be inserted here
43
+
),
44
+
),
45
+
),
46
+
),
47
+
);
48
+
49
+
// Now, json_encode the array because webhooks endpoint requires JSON object
// This example shows how to have your users log in via Patreon, and acquire access and refresh tokens after logging in
4
+
5
+
require_once__DIR__.'/vendor/autoload.php';
6
+
7
+
usePatreon\API;
8
+
usePatreon\OAuth;
9
+
10
+
$client_id = ''; // Replace with your data
11
+
$client_secret = ''; // Replace with your data
12
+
13
+
// Set the redirect url where the user will land after oAuth. That url is where the access code will be sent as a _GET parameter. This may be any url in your app that you can accept and process the access code and login
// You can send an array of vars to Patreon and receive them back as they are. Ie, state vars to set the user state, app state or any other info which should be sent back and forth.
25
+
26
+
// for example lets set final page which the user needs to land at - this may be a content the user is unlocking via oauth, or a welcome/thank you page
// Now place the url into a login link. Below is a very simple login link with just text. in assets/images folder, there is a button image made with official Patreon assets (login_with_patreon.php). You can also use this image as the inner html of the <a> tag instead of the text provided here
44
+
45
+
// Scopes! You must request the scopes you need to have the access token.
46
+
// In this case, we are requesting the user's identity (basic user info), user's email
47
+
// For example, if you do not request email scope while logging the user in, later you wont be able to get user's email via /identity endpoint when fetching the user details
48
+
// You can only have access to data identified with the scopes you asked. Read more at https://docs.patreon.com/#scopes
// Simply echoing it here. You can present the login link/button in any other way.
57
+
58
+
echo'<a href="'.$href.'">Click here to login via Patreon</a>';
59
+
60
+
// The below code snippet needs to be active wherever the the user is landing in $redirect_uri parameter above. It will grab the auth code from Patreon and get the tokens via the oAuth client
// Here, you should save the access and refresh tokens for this user somewhere. Conceptually this is the point either you link an existing user of your app with his/her Patreon account, or, if the user is a new user, create an account for him or her in your app, log him/her in, and then link this new account with the Patreon account. More or less a social login logic applies here.
71
+
72
+
}
73
+
74
+
// After linking an existing account or a new account with Patreon by saving and matching the tokens for a given user, you can then read the access token (from the database or whatever resource), and then just check if the user is logged into Patreon by using below code. Code from down below can be placed wherever in your app, it doesnt need to be in the redirect_uri at which the Patreon user ends after oAuth. You just need the $access_token for the current user and thats it.
75
+
76
+
// Lets say you read $access_token for current user via db resource, or you just acquired it through oAuth earlier like the above - create a new API client
77
+
78
+
$api_client = newAPI($access_token);
79
+
80
+
// Return from the API can be received in either array, object or JSON formats by setting the return format. It defaults to array if not specifically set. Specifically setting return format is not necessary. Below is shown as an example of having the return parsed as an object. If there is anyone using Art4 JSON parser lib or any other parser, they can just set the API return to JSON and then have the return parsed by that parser
81
+
82
+
// You dont need the below line if you simply want the result as an array
83
+
$api_client->api_return_format = 'object';
84
+
85
+
// Now get the current user:
86
+
$patron_response = $api_client->fetch_user();
87
+
88
+
// At this point you can do anything with the user return. For example, if there is no return for this user, then you can consider the user not logged into Patreon. Or, if there is return, then you can get the user's Patreon id or pledge info. For example if you are able to acquire user's id, then you can consider the user logged into Patreon.
0 commit comments