lukapeharda / kourses-php
Kourses API PHP library
Installs: 1 107
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.6.0
- guzzlehttp/guzzle: 6.3.*|6.5.*|^7.2
- illuminate/support: ~5.5|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
README
The Kourses PHP library provides access to Kourses API from applications written in PHP. It includes a pre-defined set of classes for API resources such as members
, memberships
and permissions
.
Requirements
PHP 5.6.0 and later.
Installation
You can install the library via Composer. Run the following command:
composer require lukapeharda/kourses-php
To use the library, use Composer's autoload:
require_once('vendor/autoload.php');
Getting Started
Visit Kourses Settings / API keys page and generate API key. Each key is tied to only one of your website. Copy your API key as you won't be able to access the token later on.
API key should go in HTTP Authorization
header as a bearer token:
Authorization: Bearer GENERATED_API_TOKEN
First initialize a client and set the token
$kourses = new KoursesPhp\Client(); $kourses->setApiKey('GENERATED_API_TOKEN');
If you want to access beta environment of Kourses app, use setApiBaseUrl
method to change the URL:
$kourses->setApiBaseUrl('https://app.kourses-beta.com/api/');
Fetching memberships
To fetch all published memberships use memberships
resource and the all
method:
$memberships = $kourses->memberships->all();
Returned data will be paginated (default number of memberships per page is 100). In order to change number of memberships per page or current page use page
and per_page
params (respectively):
$memberships = $kourses->memberships->all([ 'per_page' => 10, 'page' => 2, ]);
You can iterate over $memberships
to get KoursesPhp\Membership
entities.
There are several helpful methods to handle pagination:
$memberships->getCurrentPage(); // returns current page number $memberships->getLastPage(); // returns last page number $memberships->getTotal(); // returns total number of memberships $memberships->getFrom(); // returns current page items range start $memberships->getTo(); // returns current page items range end $memberships->getPerPage(); // returns number of memberships per page
Creating members
Use create
method on the members
resource:
$member = $kourses->members->create([ 'email' => 'john.doe@example.com', 'first_name' => 'John', 'last_name' => 'Doe', ]);
If member was found (based on their email address) they will be updated with the rest of the given data.
You may set a list of memberships
for which the member should get access to in the same API call:
$member = $kourses->members->create([ 'email' => 'john.doe@example.com', 'first_name' => 'John', 'last_name' => 'Doe', 'memberships' => ['MEMBERSHIP#1', 'MEMBERSHIP#2'], ]); // or $member = $kourses->members->create([ 'email' => 'john.doe@example.com', 'first_name' => 'John', 'last_name' => 'Doe', 'memberships' => [ [ 'id' => 'MEMBERSHIP#1' ], [ 'id' => 'MEMBERSHIP#2' ] ], ]);
Sending activation email
By default, when new member is created they will be sent an activation email.
To disable this set send_activation_notification
to 0
:
$member = $kourses->members->create([ 'email' => 'john.doe@example.com', 'first_name' => 'John', 'last_name' => 'Doe', 'send_activation_notification' => 0, ]);
Setting permission expiry dates
For each membership you can set future expiry date using memberships_ends_at
param or nested memberships[0][ends_at]
. Date given needs to be in YYYY-MM-DD
or YYYY-MM-DD hh:mm:ss
format:
$member = $kourses->members->create([ 'email' => 'john.doe@example.com', 'first_name' => 'John', 'last_name' => 'Doe', 'memberships' => ['MEMBERSHIP#1', 'MEMBERSHIP#2'], 'memberships_ends_at' => [ 'MEMBERSHIP#1' => '2030-04-05', ], ]); // or $member = $kourses->members->create([ 'email' => 'john.doe@example.com', 'first_name' => 'John', 'last_name' => 'Doe', 'memberships' => [ [ 'id' => 'MEMBERSHIP#1', 'ends_at' => '2030-04-05' ], [ 'id' => 'MEMBERSHIP#2' ] ], ]);
Skipping drip schedule
For each membership you can disable drip schedule using memberships_skip_drip_schedule
param or nested memberships[0][skip_drip_schedule]
:
$member = $kourses->members->create([ 'email' => 'john.doe@example.com', 'first_name' => 'John', 'last_name' => 'Doe', 'memberships' => ['MEMBERSHIP#1', 'MEMBERSHIP#2'], 'memberships_skip_drip_schedule' => [ 'MEMBERSHIP#1' => 0, ], ]); // or $member = $kourses->members->create([ 'email' => 'john.doe@example.com', 'first_name' => 'John', 'last_name' => 'Doe', 'memberships' => [ [ 'id' => 'MEMBERSHIP#1', 'skip_drip_schedule' => 0 ], [ 'id' => 'MEMBERSHIP#2' ] ], ]);
By default it will honor the set drip schedule.
Running membership's email integrations
For each membership you can enable running of email integrations using memberships_run_email_integrations
param (or nested memberships[0][run_email_integrations]
):
$member = $kourses->members->create([ 'email' => 'john.doe@example.com', 'first_name' => 'John', 'last_name' => 'Doe', 'memberships' => ['MEMBERSHIP#1', 'MEMBERSHIP#2'], 'memberships_run_email_integrations' => [ 'MEMBERSHIP#1' => 1, ], ]); // or $member = $kourses->members->create([ 'email' => 'john.doe@example.com', 'first_name' => 'John', 'last_name' => 'Doe', 'memberships' => [ [ 'id' => 'MEMBERSHIP#1', 'run_email_integrations' => 1 ], [ 'id' => 'MEMBERSHIP#2' ] ], ]);
Fetching allowed memberships for a member
Use memberMemberships
resource and all
method to fetch a list of all memberships that member was granted access to:
$memberships = $kourses->memberMemberships->all([ 'member' => 'MEMBER#1', ]);
You can even use member's email address to fetch the memberships:
$memberships = $kourses->memberMemberships->all([ 'member' => 'john.doe@example.com', ]);
Returned data will be paginated (default number of memberships per page is 100). In order to change number of memberships per page or current page use page
and per_page
params (respectively):
$memberships = $kourses->memberMemberships->all([ 'member' => 'MEMBER#1', 'per_page' => 10, 'page' => 2, ]);
Generate one-click login link for a member
Use memberLoginLink
resource and its create
method to generate temporary signed login link.
$loginLink = $koures->memberLoginLink->create([ 'member' => 'MEMBER#1', ]);
Returned $loginLink
object will have a login_link
property which you can serve to your user in order for them to log in. expires_at
property which holds the timestamp (ISO-8601) then the login link will expire.
Signed link will expired in 5 mins (300 seconds).
You can even use member's email address to generate the link:
$loginLink = $kourses->memberLoginLink->create([ 'member' => 'john.doe@example.com', ]);
If you wish to redirect the member after successful login specify a redirect
param with relative path:
$loginLink = $kourses->memberLoginLink->create([ 'member' => 'john.doe@example.com', 'redirect' => 'account/profile', ]);
Grant a permission
To grant a permission you need to call create
method on permissions
resource and provide a member ID (or their email address) and a membership ID:
$status = $kourses->permissions->create([ 'member' => 'MEMBER#1', 'membership' => 'MEMBERSHIP#1', ]);
You can add and extra ends_at
param to specify permission expiry date. Date given needs to be in YYYY-MM-DD
or YYYY-MM-DD hh:mm:ss
format:
$status = $kourses->permissions->create([ 'member' => 'MEMBER#1', 'membership' => 'MEMBERSHIP#1', 'ends_at' => '2030-04-05', ]);
If you wish to skip drip schedule set for a given membership use skip_drip_schedule
and set it to 0
:
$status = $kourses->permissions->create([ 'member' => 'MEMBER#1', 'membership' => 'MEMBERSHIP#1', 'skip_drip_schedule' => 0, ]);
If you wish to run email integrations for a given membership use run_email_intgrations
and set it to 1
:
$status = $kourses->permissions->create([ 'member' => 'MEMBER#1', 'membership' => 'MEMBERSHIP#1', 'run_email_intgrations' => 0, ]);
By default the set drip schedule will be honored.
Revoke a permission
To revoke a permission you need to call delete
method on permissions
resource and provide a member ID (or their email address) and a membership ID:
$status = $kourses->permissions->delete([ 'member' => 'MEMBER#1', 'membership' => 'MEMBERSHIP#1', ]);
You can add and extra ends_at
param to specify permission expiry date. Date given needs to be in YYYY-MM-DD
or YYYY-MM-DD hh:mm:ss
format:
$status = $kourses->permissions->delete([ 'member' => 'MEMBER#1', 'membership' => 'MEMBERSHIP#1', 'ends_at' => '2030-04-05', ]);
Documentation
Check out examples folder for some basic use cases. Full documentation is available on developer.kourses.com.