PHP Quickstart
Currently, the PHP SDK is in beta. Please contact us if you have any questions or feedback.
You can follow the public Github repository to learn more about the current state of the SDK - https://github.com/permitio/permit-php
Getting Started
Here's a quick guide to get you started with the Permit PHP SDK.
Installation & Usage
Requirements
PHP 7.4 and later. Should also work with PHP 8.0.
Composer
To install the bindings via Composer, add the following to composer.json
:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/permitio/permit-php.git"
}
],
"require": {
"permitio/permit-php": "*@dev"
}
}
Then run composer install
Manual Installation
Download the files and include autoload.php
:
<?php
require_once('/path/to/OpenAPIClient-php/vendor/autoload.php');
Example
Please follow the installation procedure and then run the following:
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use React\Http\Server;
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Socket\SocketServer;
$permitToken = '<Place your Permit Token here>';
$pdpUrl = 'https://cloudpdp.api.permit.io';
$apiUrl = 'https://api.permit.io';
$port = 4000;
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToken($permitToken)->setHost($apiUrl);
// Get the scope of the API key
$apiInstance = new OpenAPI\Client\Api\APIKeysApi(
new GuzzleHttp\Client(),
$config
);
try {
$scope = $apiInstance->getApiKeyScope();
print_r($scope);
} catch (Exception $e) {
echo 'Exception when calling APIKeys';
}
$usersInstance = new OpenAPI\Client\Api\UsersApi(
new GuzzleHttp\Client(),
$config
);
// Create user with the given data
$user_create = new \OpenAPI\Client\Model\UserCreate([
'key' => 'raz-cohen',
'email' => 'raz@permit.io',
'first_name' => 'Raz',
'last_name' => 'Cohen',
]);
try {
$result = $usersInstance->createUser($scope->getProjectId(), $scope->getEnvironmentId(), $user_create);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling UsersApi->createUser: ', $e->getMessage(), PHP_EOL;
}
// Permit check function
$pdpConfig = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToken($permitToken)->setHost($pdpUrl);
$pdpInstance = new OpenAPI\Client\Api\PDP\AuthorizationAPIApi(
new GuzzleHttp\Client(),
$pdpConfig
);
// Create a query object - this is the data we want to check
// in this case we're checking if the user 'raz-cohen' is allowed to read a document
$query = new \OpenAPI\Client\Model\PDP\Query([
'user' => ['key' => 'raz-cohen'],
'action' => 'read',
'resource' => [
'type' => 'document',
'tenant' => 'default'
]
]);
try {
$is_allowed = $pdpInstance->isAllowedAllowedPost(
$query,
);
// We'll print the result to the console
if ($is_allowed->getAllow()) {
echo "Permitted\n";
} else {
echo "Not Permitted\n";
}
} catch (Exception $e) {
echo 'Exception when calling PDP\AuthorizationAPIApi->isAllowedAllowedPost: ', $e->getMessage(), PHP_EOL;
}
Come Chat With Us!
You can contact us in our Slack Community for any questions or feedback here -