PHP code example of tiao2 / php-tools

1. Go to this page and download the library: Download tiao2/php-tools library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tiao2 / php-tools example snippets


use PhpTools\SSO\AuthenticatorInterface;

class MyAuthenticator implements AuthenticatorInterface
{
    public function authenticate(string $username, string $password): ?int
    {
        // Query database, verify password
        // Return user ID or null
    }
}

$sso = SSO::fromEnv(new MyAuthenticator());
$result = $sso->login('admin', 'password');
// Returns ['token' => '...', 'authcode' => '...']

$postManager = new PostManager($sso, $pdo, $aclChecker);
$postId = $postManager->create('Hello World', 'Content...', $token, $authCode);

$router->get('/articles', function (Request $req) {
    // Public endpoint, no auth n (Request $req) {
    // Requires auth (AuthMiddleware ensures $req->userId is set)
    $data = $req->getJsonBody();
    // Create article...
    return Response::json(['id' => 123], 201);
});
bash
composer