The Mavryx PHP library facilitates the use of the Mavryx API from PHP.
To install mavryx/mavryx-php-lib run the require command.
composer require mavryx/mavryx-php-lib
Configuration for plain PHP
$config = [
'exceptions' => true, // if true trow exceptionsm if false return without exceptions
'endpoints' => [ // List of endpoints
'OAuth2' => 'auth-dev.mavryx.solutions', // authorization endpoint
]
];
$api = new \Mavryx\Mavryx($config);
Authorize application to work on behalf of the user.
The application authorization process is divided into two stages:
- Redirecting the user to the authentication endpoint.
- Handling the response and exchanging the code for the application token.
Redirecting the user to the authentication endpoint
// Your app client settings
$client_id = "ea0075c-ac22-4908-81f1-d567ff689c2c";
$redirect_uri = "https://someapp.co.uk/authenticate";
$state = md5(time());
//save here state
$url = $api->OAuth2->getAuthorizationUrl([
'client_id' => $client_id,
'response_type' => 'code',
'state' => $state,
'redirect_uri' => $redirect_uri,
]);
/**
* Example url:
https://auth-dev.mavryx.software/oauth2/signin?client_id=ea0075c-ac22-4908-81f1-d567ff689c2c&response_type=code&state=123131234&redirect_uri=https://someapp.co.uk/authenticate
*/
header("Location: ".$url);
die();
If the user logs into the platform successfully, they will be redirected back to your application’s endpoint defined in the redirect_uri, and your application should validate the response and request the authorization server to exchange the code for a token.
$code = $_GET('code');
$state = $_GET('state');
// Validate state here
$client_id = "ea0075c-ac22-4908-81f1-d567ff689c2c";
$client_secret="your_app_secret";
$response = $api->OAuth2->authorize([
'client_id' => $client_id,
'client_secret' => $client_secret,
'grant_type' => 'authorization_code',
'code' => $code,
]);
$userToken = $response->getAccessToken();
if(!$userToken) {
// something really bad happen. No network?
die();
}
if(!$userToken->isValid())
{
// token is invalid
die();
}
// here you can save user token in session for example
// or use it now for example to get current user details
$user = $api->OAuth2->setToken($userToken)->user()->me([])->item();
echo $user->client_id;
// will return user client_id (ex. someEmail@some.com