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');
}You can also pass a ConfigResponseData object directly:
$sdk = ConvertSDK::create([
'data' => [
'account_id' => '100123456',
'project' => [
'id' => '10045678',
// ... full project config
],
],
]);You must provide either sdkKey or data. If both are missing, an InvalidArgumentException is thrown.
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
HttpClientDiscovery::find(). - PSR-17 factories (request factory, stream factory) are auto-discovered automatically.
- 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.
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. Ifdatais provided, uses it immediately. - 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 about 1 month ago