Examples / How To
Data Indexing
List Existing Attributes
7min
this guide assumes you have installed the latest version of klevu/php sdk , either via composer or from source, and autoloading is functioning as expected examples in this guide are for vanilla php and, as such, will use new to instantiate objects if you are using a modern framework such as magento or symfony, you should utilise your object manager for dependency injection and preferencing of interfaces where possible retrieve all attributes for an account to retrieve attributes used by klevu's xml feed monitoring service, first locate your klevu api and rest auth keys and create an accountcredentials object (see quickstart for steps) next, instantiate an indexing\attributeservice object and call the get method to return a collection of all attributes registered in klevu indexing the get method will throw exceptions if any issues arise, so you should catch and handle these appropriately at this point, you will have an attributeiterator object containing attribute models for all registered attributes, which can be filtered and used in subsequent operations \<?php declare(strict types=1); use klevu\phpsdk\exception\api\badrequestexception; use klevu\phpsdk\exception\api\badresponseexception; use klevu\phpsdk\exception\validationexception; use klevu\phpsdk\model\accountcredentials; use klevu\phpsdk\service\indexing\attributesservice; $accountcredentials = new accountcredentials( jsapikey "klevu 123456780", restauthkey "abcde1234567890", ); $attributesservice = new attributesservice( // inject any relevant dependencies, such as http client; logger; or user agent provider ); try { $attributes = $attributesservice >get( accountcredentials $accountcredentials, ); } catch (validationexception $exception) { // one or more supplied arguments are missing or invalid } catch (badrequestexception $exception) { // klevu api rejected the request as invalid } catch (badresponseexception $exception) { // klevu api responded in an unexpected manner } filter attributes if you need to filter the return value of get , for example to remove immutable attributes or to identify attributes of a specific data type, you can call the filter method with your callback note filtering an iterator in this way will return a copy, and will not modify the data contained within the original return object \<?php // snip $customattributes = $attributes >filter( callback static fn (attributeinterface $attribute) bool => !$attribute >isimmutable(), ); retrieve a specific attribute by name to retrieve a registered attribute by name, first locate your klevu api and rest auth keys and create an accountcredentials object (see quickstart for steps) next, instantiate an indexing\attributeservice object and call the getbyname method with your credentials and the name of the attribute being search for the getbyname method will throw exceptions if any issues arise, so you should catch and handle these appropriately at this point, you will have either an attribute object or null note this method does not map to a discrete api endpoint its purpose is essentially syntactic sugar, wrapping get and filter calls described above the attributename value is not case sensitive in klevu's indexer and, therefore, is not case sensitive in name lookups in the php sdk for example, a request for example attribute may return example attribute or other cased variations; though only one attribute (at most) will ever be returned from the api the attributename property will reflect the case used at time of registration \<?php declare(strict types=1); use klevu\phpsdk\exception\api\badrequestexception; use klevu\phpsdk\exception\api\badresponseexception; use klevu\phpsdk\exception\validationexception; use klevu\phpsdk\model\accountcredentials; use klevu\phpsdk\service\indexing\attributesservice; $accountcredentials = new accountcredentials( jsapikey "klevu 123456780", restauthkey "abcde1234567890", ); $attributesservice = new attributesservice( // inject any relevant dependencies, such as http client; logger; or user agent provider ); try { $attribute = $attributesservice >getbyname( accountcredentials $accountcredentials, attributename 'example attribute', ); } catch (validationexception $exception) { // one or more supplied arguments are missing or invalid } catch (badrequestexception $exception) { // klevu api rejected the request as invalid } catch (badresponseexception $exception) { // klevu api responded in an unexpected manner } source code reference api\model\indexing\ attributeinterface exception\api\ badrequestexception exception\api\ badresponseexception exception\ validationexception model\ accountcredentials service\indexing\ attributesservice