PHP code example of gsu-cetloe / brightspace-api-core-php

1. Go to this page and download the library: Download gsu-cetloe/brightspace-api-core-php 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/ */

    

gsu-cetloe / brightspace-api-core-php example snippets


use Brightspace\Api\Core\Client\CoreApiClient;
use Brightspace\Api\Core\Client\CoreClient;
use Brightspace\Api\Core\Config;
use Gadget\Cache\CacheItemPool;
use Gadget\Http\Client\Client;
use Gadget\Http\Message\MessageFactory;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\HttpFactory;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$factory = new HttpFactory();

$http = new Client(
    new GuzzleClient(['allow_redirects' => false, 'http_errors' => false]),
    new MessageFactory($factory, $factory, $factory, $factory, $factory, $factory),
    new CacheItemPool(new FilesystemAdapter())
);

$config = new Config(
    d2lHost: 'lms.example.edu',
    rootOrgUnit: 6606,
    clientId: 'my-client-id',
    clientSecret: 'my-client-secret',
    redirectUri: 'https://app.example.edu/callback',
    scope: 'core:*:*',
    username: 'svc-brightspace',
    password: 'secret',
    mfa: 'JBSWY3DPEHPK3PXP'
);

$coreApi = new CoreApiClient($http, $config);
$core = new CoreClient($http, $coreApi);

$accessToken = $core->getAccessToken();

$core->login();                       // true once the session cookies are in place
$authCode = $coreApi->createAuthCode(); // needs a live session
$token = $core->createToken($authCode); // or $core->createToken() to log in and get a code first
$fresh = $core->refreshToken($token->refreshToken);

use Brightspace\Api\Core\Message\MessageHandler;
use Gadget\Http\Message\RequestBuilder;
use Gadget\Io\Cast;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

/** @extends MessageHandler<array<string,mixed>> */
final class WhoAmIHandler extends MessageHandler
{
    protected function createRequest(RequestBuilder $requestBuilder): ServerRequestInterface
    {
        return $requestBuilder
            ->setMethod('GET')
            ->setUri('d2l://lp/users/whoami')
            ->getRequest();
    }

    protected function handleResponse(
        ResponseInterface $response,
        ServerRequestInterface $request
    ): mixed {
        if ($response->getStatusCode() !== 200) {
            throw new \RuntimeException("Status code: " . $response->getStatusCode());
        }
        return $this->jsonToArray($response);
    }
}

use Brightspace\Api\Core\Client\ApiClient;

final class UsersClient extends ApiClient
{
    /** @return array<string,mixed> */
    public function whoAmI(): array
    {
        return $this->invoke(new WhoAmIHandler());
    }
}

$users = new UsersClient($http, $config, $core);
$me = $users->whoAmI();

use Brightspace\Api\Core\Model\PagedResultSet;

$allUsers = PagedResultSet::forEach(
    fn(string|null $bookmark): PagedResultSet => $users->listPage($bookmark)
);

foreach ($allUsers as $user) {
    // ...
}

return [
    // ...
    Gadget\Cache\GadgetCacheBundle::class => ['all' => true],
    Gadget\Http\GadgetHttpBundle::class => ['all' => true],
    Gadget\Oauth\GadgetOauthBundle::class => ['all' => true],
    Brightspace\Api\Core\BrightspaceApiCoreBundle::class => ['all' => true],
];