plug

Learn how to create and configure the SDK.

This static factory method creates an SDK instance wired with sensible defaults.

The HTTP client and PSR-17 factories are auto-discovered when not provided.

Signature

This method has the following signature:

public static function plug(    string $appId,    ApiKey|string $apiKey,    IdentityStore $storage,    ?IdentityResolver $identity = null,    ?string $baseEndpointUrl = null,    int $tokenDuration = 86400,    ?ContentProvider $contentProvider = null,    ?RequestContext $context = null,    ?ClientInterface $httpClient = null,    ?RequestFactoryInterface $requestFactory = null,    ?StreamFactoryInterface $streamFactory = null,    ?LoggerInterface $logger = null,): self

This method returns a configured SDK instance.

Example

Here is a basic example of how to use this method:

123456789
<?phpuse Croct\Plug\Croct;use Croct\Plug\CookieStorage;
$croct = Croct::plug(    appId: 'APPLICATION_ID',    apiKey: 'API_KEY',    storage: CookieStorage::fromGlobals(),);

Parameters

The following list describes the supported parameters:

appId
string

The ID of the application, available on the Integration page of your application.

apiKey
ApiKey|string

The API key used to authenticate server-side requests, passed as a serialized string or an ApiKey instance.

You can create one on the Integration page of your application. When the key carries a private key, the SDK issues signed tokens for the visitor.

storage

The storage that persists the visitor session, such as the client ID and user token.

identity(optional)

A resolver that keeps the visitor in sync with the authenticated user of your application.

baseEndpointUrl(optional)
string

The base URL to use for the API calls.

The default is null, which uses the production endpoint. This option is helpful for testing, allowing you to point the SDK to a mock server.

Default:null
tokenDuration(optional)
int

The lifetime of the user token, in seconds.

Default:86400
contentProvider(optional)

A source of default content used as fallback when dynamic content is unavailable.

Auto-discovered from the committed content when not set.

Default:null
context(optional)
RequestContext

The request signals used to personalize content, such as the URL, referrer, IP, user agent, and preferred locale.

Built from the PHP superglobals when not set.

Default:null
httpClient(optional)

A PSR-18 HTTP client used to call the Croct API.

Auto-discovered when not set.

Default:null
requestFactory(optional)

A PSR-17 request factory.

Auto-discovered when not set.

Default:null
streamFactory(optional)

A PSR-17 stream factory.

Auto-discovered when not set.

Default:null
logger(optional)

A PSR-3 logger that records transport failures.