The Factory
January 3, 2026 ยท View on GitHub
In some cases, you want to load a provisioning URI and get the corresponding OTP object. That is why we created a factory.
<?php
use OTPHP\Factory;
use OTPHP\InternalClock;
$clock = new InternalClock(); // Or your own PSR-20 Clock implementation
// Load from a provisioning URI
$otp = Factory::loadFromProvisioningUri(
'otpauth://totp/alice%40google.com?secret=JBSWY3DPEHPK3PXP&foo=bar',
$clock
);
// The variable $otp is now a valid TOTPInterface or HOTPInterface object with all parameters set (including custom parameters)
Note: In v11.4+, passing a Clock parameter is optional but recommended. In v12.0, it will become mandatory for TOTP objects. See UPGRADE_v11-v12.md for more details.
Issuer Handling
Since v11.4, the Factory correctly handles provisioning URIs where the issuer in the label differs from the issuer parameter, as commonly generated by Microsoft Office 365 and other providers. The issuer parameter takes precedence if both are present.