Initialization
ConvertSDK::create() factory, synchronous init, PSR discovery
Key difference from the JavaScript SDK: The PHP SDK is fully synchronous. There is no
onReady()Promise. The SDK is ready to use immediately afterConvertSDK::create()returns. UseisReady()to verify initialization succeeded.
ConvertSDK::create() Factory
ConvertSDK is a static factory. Use ConvertSDK::create() — the constructor is private and cannot be called directly.
use ConvertSdk\ConvertSDK;
$sdk = ConvertSDK::create([
'sdkKey' => 'your-sdk-key',
]);Using SDK Key (Remote Config Fetch)
The SDK fetches the project configuration from Convert's CDN and caches it using the provided PSR-16 cache.
use ConvertSdk\ConvertSDK;
$sdk = ConvertSDK::create([
'sdkKey' => 'your-sdk-key',
'sdkKeySecret' => 'xxx', // required when using an authenticated SDK key
'environment' => 'staging',
'dataRefreshInterval' => 300000, // in milliseconds (5 minutes)
]);
if ($sdk->isReady()) {
// SDK is ready — create a user context and run experiences
$context = $sdk->createContext('visitor-123');
}Using Static Configuration
Provide a project configuration array directly. The config data can be fetched from https://cdn-4.convertexperiments.com/api/v1/config/{account_id}/{project_id}.
use ConvertSdk\ConvertSDK;
$sdk = ConvertSDK::create([
'data' => [/* your static project configuration array */],
'environment' => 'staging',
]);
if ($sdk->isReady()) {
$context = $sdk->createContext('visitor-123');
}A raw array is wrapped in a ConfigResponseData for you — the shape is the CDN config response:
$sdk = ConvertSDK::create([
'data' => [
'account_id' => '100123456',
'project' => [
'id' => '10045678',
// ... full project config
],
],
]);You can also pass an already-constructed OpenAPI\Client\Model\ConfigResponseData instance instead of the array.
You must provide either sdkKey or data. If both are missing, an InvalidArgumentException is thrown.
QA Debug Token
Passing a debugToken widens the config fetch to every non-archived experience (drafts and paused ones included) and bypasses the config cache entirely:
$sdk = ConvertSDK::create([
'sdkKey' => 'your-sdk-key',
'debugToken' => 'your-qa-debug-token',
]);See Configuration → QA Debug Token for the full behaviour and the production warning.
Checking Readiness
if ($sdk->isReady()) {
echo "Convert SDK is ready!\n";
// Now you can start creating contexts for visitors
}isReady() returns true when the SDK has successfully fetched and processed configuration data.
PSR Discovery
The SDK auto-discovers PSR-18 HTTP client and PSR-17 request/stream factories using php-http/discovery. During ConvertSDK::create():
- PSR-18 HTTP client is auto-discovered via
Http\Discovery\Psr18ClientDiscovery::find(). - PSR-17 factories (request factory, stream factory) are auto-discovered via
Http\Discovery\Psr17FactoryDiscovery. - PSR-16 cache defaults to
ConvertSdk\Cache\ArrayCache(in-memory) if not provided.
No adapter code is needed — install any PSR-18 compliant client and the SDK finds it automatically. When discovery finds nothing, ConvertSDK::create() throws a RuntimeException telling you to install a client (e.g. guzzlehttp/guzzle ^7).
What Happens Under the Hood
When you call ConvertSDK::create($config):
- Configuration — The SDK reads the
$configarray and sets defaults for any options not specified. - PSR Discovery — Auto-discovers PSR-18 HTTP client and PSR-17 factories.
- Manager Creation — Creates internal managers:
LogManager,EventManager,ApiManager,DataManager,RuleManager,BucketingManager,ExperienceManager,FeatureManager,SegmentsManager. - Data Persistence Wiring — If a PSR-16
cacheis provided, it is wired as the visitor data store automatically. - Initialization — If
sdkKeyis present, fetches config from Convert's CDN, reading and writing the PSR-16 config cache along the way (both are skipped whendebugTokenis set). Ifdatais provided, it is validated and used immediately with no fetch. - Shutdown Handler —
register_shutdown_functionis registered to flush pending tracking events when the PHP script ends. In PHP-FPM, it callsfastcgi_finish_request()first so the response is sent before flushing. - Ready State —
isReady()returnstrue.
Next Steps
- Configuration — Full SDK config options reference
- Code Examples — All SDK methods with examples
Updated 21 days ago