sentimo/php-client

用于与Sentimo API交互以执行情感分析的PHP客户端。

1.1.0 2024-09-13 03:24 UTC

README

欢迎使用Sentimo PHP客户端SDK!此SDK提供易于使用的接口来与Sentimo API交互,让您轻松发布和检索客户评价。

目录

安装

要安装Sentimo PHP客户端SDK,请使用Composer

composer require sentimo/php-client

用法

初始化客户端

首先,您需要初始化客户端以与Sentimo API交互。ClientFactory类帮助您使用API密钥创建配置好的客户端实例。

use Sentimo\Client\HttpClient\ClientFactory;

$apiKey = 'your-api-key-here';
$clientFactory = new ClientFactory();
$client = $clientFactory->createClient($apiKey);

发布评价

要发布评价,您可以使用postReviews方法。此方法接受一个ReviewInterface对象数组和一个可选的channel参数。

use Sentimo\Client\Api\Data\ReviewInterface;
use Sentimo\Client\Api\Data\AuthorInterface;
use Sentimo\Client\Api\Data\ProductInterface;

// Example Review, Author, and Product objects (these would be created according to your implementation)
$review = new class implements ReviewInterface {
// Implement the methods of ReviewInterface
};

$reviews = [$review];
$channel = 'your-channel'; // Optional

try {
    $postedReviewIds = $client->postReviews($reviews, $channel);
    echo 'Posted Reviews: ' . implode(', ', $postedReviewIds);
} catch (LocalizedException $e) {
    echo 'Error posting reviews: ' . $e->getMessage();
    
}

获取评价

要获取评价,您可以使用getReviews方法。您可以选择传递ReviewGetRequestParamBuilder来过滤和结构化您的请求。

use Sentimo\Client\RequestParam\ReviewGetRequestParamBuilder;

$paramBuilder = new ReviewGetRequestParamBuilder();
$paramBuilder->setExternalIds(['external-id-1', 'external-id-2'])
             ->setModerationStatus('approved');

try {
    $reviews = $client->getReviews($paramBuilder, true); // true to fetch all pages
    foreach ($reviews as $review) {
        // Process each review
    }
} catch (LocalizedException $e) {
    echo 'Error fetching reviews: ' . $e->getMessage();
}

客户端配置

ClientFactory会自动使用Symfony DI容器配置客户端实例。您只需提供API密钥,工厂将处理其余部分。

如果您需要进一步自定义客户端配置,您可以修改ContainerFactory或使用Symfony依赖注入组件。

错误处理

SDK在API调用过程中抛出LocalizedException以处理错误。您应该在应用程序代码中捕获和处理这些异常。

try {
    $postedReviewIds = $client->postReviews($reviews, $channel);
} catch (LocalizedException $e) {
    // Handle the error
}

Client类上的getErrors方法也可以用来检索操作过程中遇到的错误列表。

$errors = $client->getErrors();
if (!empty($errors)) {
    foreach ($errors as $error) {
        echo $error . PHP_EOL;
    }
}

贡献

我们欢迎贡献!如果您发现错误或想添加新功能,请在GitHub上打开一个问题或提交一个pull request。

许可证

此SDK在MIT许可证下发布。有关更多信息,请参阅LICENSE文件。