https://github.com/guzzle/guzzle
use GuzzleHttp\Client;
$baseUrl = 'https://<TRACARDI_API_URL>';
$client = new Client([
'base_uri' => $baseUrl
]);
https://github.com/thephpleague/oauth2-client
use Tracardi\TracardiPhpSdk\OAuth2\Provider\TracardiProvider;
$provider = new TracardiProvider(['baseUrl' => $baseUrl]);
$accessToken = $provider->getAccessToken('password', [
'username' => '<USERNAME>',
'password' => '<PASSWORD>'
]);
use Http\Adapter\Guzzle6\Client as ClientAdapter;
use Tracardi\TracardiPhpSdk\Http\ApiClient\ApiClient;
$psrClient = new ClientAdapter($client);
$apiClient = ApiClient::withProvider($psrClient, $provider, $accessToken);
use Tracardi\TracardiPhpSdk\Tracardi;
$profile = Tracardi::withDefaultSerializer($apiClient)
->profiles()
->getProfile('<ID>');
It is not strictly required to use the League OAuth provider, alternatively you can instantiate the API client with an HTTP client only. It is your own responsibility that this HTTP client performs authenticated requests.
This flow requires some PSR-7 request factory to be present, e.g. composer require nyholm/psr7
.
See the php-http docs for more information.
use Http\Adapter\Guzzle6\Client as ClientAdapter;
use Tracardi\TracardiPhpSdk\Http\ApiClient\ApiClient;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
// When using Guzzle you could create your own custom middleware
// that fetches the access token from e.g. the database.
$middleware = function (callable $handler): callable {
return function (RequestInterface $request, array $options) use ($handler) {
$request = $request->withHeader('Authorization', 'Bearer ' . fetchToken());
return $handler($request, $options);
};
};
$stack = HandlerStack::create();
$stack->push($middleware);
$client = new Client([
'base_uri' => 'https://<TRACARDI_API_URL>',
'handler' => $stack
]);
$psrClient = new ClientAdapter($client);
$apiClient = ApiClient::withClient($psrClient);