Examples / How To

Modify User-Agent In Request Headers

4min
introduction the useragentprovider is responsible for collating and returning all configured components separated by a space character this class is a composite provider, that can be configured via di to inject additional providers into the constructor which are included in the user agent header value used in api calls there should be no need to replace this class with your own implementation, and doing so may break other packages which inject their own providers instead, create additional class(es) implementing the useragentproviderinterface which return the user agent information (either new components or additional information for an existing component, where supported) inject additional components first, create a new class which implements provider\useragentproviderinterface and add the execute method, returning the desired string if you wish to allow other modules to inject their own system information into your component, you can should implement the provider\composableuseragentproviderinterface interface, which extends the base interface you can also use the provider\composableuseragentprovidertrait to include implementation of child providers basic \<?php declare(strict types=1); namespace acme\mymodule\provider\useragent; use klevu\phpsdk\provider\useragentproviderinterface; class phpsdkuseragentprovider implements useragentproviderinterface { / @return string / public function execute() string { return 'mymodule/1 0 0'; } } composable \<?php declare(strict types=1); namespace acme\mymodule\provider\useragent; use klevu\phpsdk\provider\composableuseragentproviderinterface; use klevu\phpsdk\provider\composableuseragentprovidertrait; use klevu\phpsdk\provider\useragentproviderinterface; class myuseragentprovider implements composableuseragentproviderinterface { use composableuseragentprovidertrait; / @param array\<string, useragentproviderinterface> $systeminformationproviders / public function construct( array $systeminformationproviders, ) { array walk( array $systeminformationproviders, callback function (mixed $systeminformationprovider, int|string $identifier) void { $this >adduseragentprovider( useragentprovider $systeminformationprovider, identifier $identifier, ); }, ); } / @return string / public function execute() string { return sprintf( 'mymodule/1 0 0 (%s)', implode( separator ' ', array array map( callback static fn (useragentproviderinterface $systeminformationprovider) string => ( $systeminformationprovider >execute() ), array $this >useragentproviders, ), ), ); } } once you have your user agent provider, create a new instance of useragentprovider and inject it into the constructor note if you are integrating into a modern framework, you should implement this via your dependency injection system this user agent provider instance can then be passed into any service which interacts with klevu's apis \<?php declare(strict types=1); use acme\mymodule\provider\useragent\myuseragentprovider; use klevu\phpsdk\provider\useragentprovider; use klevu\phpsdk\service\account\accountfeaturesservice; use klevu\phpsdk\service\account\accountlookupservice; use klevu\phpsdk\service\account\updatestorefeedurlservice; use klevu\phpsdk\service\analytics\collectservice as analyticscollectservice; use klevu\phpsdk\service\indexing\attributesservice; use klevu\phpsdk\service\indexing\batch\deleteservice as batchdeleteservice; use klevu\phpsdk\service\indexing\batchservice; $useragentprovider = new useragentprovider( useragentproviders \[ new myuseragentprovider(), ], ); // or, if using the composable templates $useragentprovider = new useragentprovider( useragentproviders \[ new myuseragentprovider( systeminformationproviders \[ new mysysteminformationprovider(), ], ), ], ); // account $accountfeatureservice = new accountfeaturesservice( useragentprovider $useragentprovider, ); $accountlookupservice = new accountlookupservice( useragentprovider $useragentprovider, ); $updatestorefeedurlservice = new updatestorefeedurlservice( useragentprovider $useragentprovider, ); // analytics $collectservice = new analyticscollectservice( useragentprovider $useragentprovider, ); // indexing $attributesservice = new attributesservice( useragentprovider $useragentprovider, ); $batchservice = new batchservice( useragentprovider $useragentprovider, ); $batchdeleteservice = new batchdeleteservice( useragentprovider $useragentprovider, ); in the above scenario, the expected user agent string result would look like klevu php sdk/0 0 0 1 (php 8 1 999) mymodule/1 0 0 or, if composble klevu php sdk/0 0 0 1 (php 8 1 999) mymodule/1 0 0 (mysystem) source code reference provider\ composableuseragentproviderinterface provider\ composableuseragentprovidertrait provider\ useragentprovider provider\ useragentproviderinterface