klevu / php-sdk
PHP 的 Klevu SDK
Requires
- php: ~8.1|~8.2|~8.3
- ext-libxml: *
- ext-simplexml: *
- linchpin/phpdocumentor-template-markdown: ^1.0
- php-http/discovery: ~1.15
- psr/http-client: *
- psr/http-message: *
- psr/log: *
Requires (Dev)
Suggests
This package is auto-updated.
Last update: 2024-09-30 10:50:27 UTC
README
Klevu PHP-SDK 是一个用于简化与 Klevu API 服务通信的小型包。
入门指南
- 在 Klevu 上创建账户.
免费试用,可完全访问所有功能。 - 从您的账户“商店设置”>“商店信息”中检索您的 JS API 密钥 和 REST AUTH 密钥。
- 使用 composer 在您的应用程序中 安装 SDK。
composer require klevu/php-sdk
- 开始构建!
以下有一些快速入门示例,以及我们开发者文档中的详细指南。
系统要求
要使用此库,您必须运行编译了 libxml
和 simplexml
扩展的 PHP 8.1。有关更多信息,请参阅PHP 文档。
您还需要一个兼容 PSR-18 的 HTTP 客户端,例如 guzzlehttp/guzzle
,它提供 psr/http-client-implementation
支持。可以在 Packagist 上找到兼容库的列表。
我们还推荐一个兼容 PSR-3 的日志库,例如 monolog/monolog
。这将允许 SDK 将其执行的活动写入您选择的任何位置。
快速入门指南
账户凭证
所有连接到 Klevu 的服务都需要 AccountCredentials 对象。
您将需要您的 Klevu 账户中的 JS API 密钥(格式为 klevu-xxxxxxx
)和 REST AUTH 密钥。
在您的应用程序中保存和访问这些凭证时,请将 REST AUTH 密钥视为任何其他敏感信息(如密码)一样处理。
如果您需要更改您的 REST AUTH 密钥,请联系我们的支持团队。
创建一个新的 Klevu\PhpSDK\Model\AccountCredentials
对象。注意,账户凭证对象是不可变的。
<?php declare(strict_types=1); // Include the composer autoloader - you may need to change the directory path require_once 'vendor/autoload.php'; use Klevu\PhpSDK\Model\AccountCredentials; $accountCredentials = new AccountCredentials( jsApiKey: '[Your JS API Key]', restAuthKey: '[Your REST AUTH KEY]', );
检索账户详情
您可以在示例部分找到更多关于检索账户信息的完整示例。
AccountLookupService 允许您检索有关您的 Klevu 账户的详细信息,包括推送和从其他服务拉取数据所需的主机名。
<?php declare(strict_types=1); // Include the composer autoloader - you may need to change the directory path require_once 'vendor/autoload.php'; use Klevu\PhpSDK\Model\AccountCredentials; use Klevu\PhpSDK\Service\Account\AccountLookupService; $accountLookupService = new AccountLookupService(); $accountCredentials = new AccountCredentials( jsApiKey: '[Your JS API Key]', restAuthKey: '[Your REST AUTH KEY]', ); $account = $accountLookupService->execute($accountCredentials); var_export($account);
它将返回如下对象。
Klevu\PhpSDK\Model\Account::__set_state(array(
'jsApiKey' => 'klevu-1234567890',
'restAuthKey' => 'ABCDE1234567890',
'platform' => 'custom',
'isActive' => true,
'companyName' => 'Klevu',
'email' => 'contact@klevu.com',
'indexingUrl' => 'indexing.ksearchnet.com',
'searchUrl' => 'cs.ksearchnet.com',
'smartCategoryMerchandisingUrl' => 'cn.ksearchnet.com',
'analyticsUrl' => 'stats.klevu.com',
'jsUrl' => 'js.klevu.com',
'tiersUrl' => 'tiers.klevu.com',
'accountFeatures' => NULL,
))
列出的每个属性都是私有的,并且通过以下方式访问:
$account->getJsApiKey();
注意,在上面的示例中,我们实例化了服务实现而不是接口。
如果您使用依赖注入,我们建议将Klevu\PhpSDK\Api\Interface\Service\Account\AccountLookupService
接口配置为Klevu\PhpSDK\Service\Account\AccountLookupService
,以及任何构造函数参数。
例如
$accountLookupService = $container->get(\Klevu\PhpSDK\Api\Service\Account\AccountLookupServiceInterface::class);
检索账户功能
在上面的示例中,返回的模型的accountFeatures
属性为空。为了加载您账户启用的功能,您需要调用以下AccountFeaturesService
,如下所示。
<?php declare(strict_types=1); // Include the composer autoloader - you may need to change the directory path require_once 'vendor/autoload.php'; use Klevu\PhpSDK\Model\AccountCredentials; use Klevu\PhpSDK\Service\Account\AccountFeaturesService; $accountFeaturesService = new AccountFeaturesService(); $accountCredentials = new AccountCredentials( jsApiKey: '[Your JS API Key]', restAuthKey: '[Your REST AUTH KEY]', ); $accountFeatures = $accountFeaturesService->execute($accountCredentials); var_export($account);
这将返回如下不可变的对象。
Klevu\PhpSDK\Model\Account\AccountFeatures::__set_state(array(
'smartCategoryMerchandising' => true,
'smartRecommendations' => true,
'preserveLayout' => true,
))
每个属性都是公开的,可以直接访问,但不能修改。
您也可以通过设置accountFeatures
属性将此对象附加到您的已加载账户模型。这不是必需的,但您可能会发现将所有数据存储在应用的单个模型中很有用。
$account->setAccountFeatures($accountFeatures); var_export($account);
Klevu\PhpSDK\Model\Account::__set_state(array(
'jsApiKey' => 'klevu-1234567890',
'restAuthKey' => 'ABCDE1234567890',
'platform' => 'custom',
'isActive' => true,
'companyName' => 'Klevu',
'email' => 'contact@klevu.com',
'indexingUrl' => 'indexing.ksearchnet.com',
'searchUrl' => 'cs.ksearchnet.com',
'smartCategoryMerchandisingUrl' => 'cn.ksearchnet.com',
'analyticsUrl' => 'stats.klevu.com',
'jsUrl' => 'js.klevu.com',
'tiersUrl' => 'tiers.klevu.com',
'accountFeatures' => Klevu\PhpSDK\Model\Account\AccountFeatures::__set_state(array(
'smartCategoryMerchandising' => true,
'smartRecommendations' => true,
'preserveLayout' => true,
)),
))
注意:与上一个示例一样,在我们的示例中,我们实例化了实现而不是接口。再次提醒,如果您应用支持依赖注入,我们建议使用映射。