数据破坏者/php-data-api-client

PHP客户端用于数据破坏者数据API。

v2.1.0 2017-12-12 12:13 UTC

This package is not auto-updated.

Last update: 2024-09-22 08:07:15 UTC


README

此库提供了数据破坏者数据API的PHP客户端实现。

要求

库需要PHP版本5.4(或更高)和Guzzle

安装

最佳安装方法是使用Composer

$ composer require databreakers/php-data-api-client

快速入门

use DataBreakers\DataApi;

// Create a new instance of Client and provide your credentials
$client = new DataApi\Client('yourAccountId', 'yourSecretKey');

// Define items attributes (do this only when items attributes aren't defined in recommender yet)
$client->addItemsAttribute('title', DataApi\DataType::TEXT, 'en', DataApi\MetaType::TITLE);
$client->addItemsAttribute('color', DataApi\DataType::TEXT, 'en');
$client->addItemsAttribute('weight', DataApi\DataType::INTEGER);

// Add some items (if you are adding multiple items, users or interactions it's much faster to use batches)
$itemsBatch = (new DataApi\Batch\EntitiesBatch())
	->addEntity('fridgeId', [
		'title' => 'Fridge',
		'color' => 'white',
		'weight' => 55
	])
	->addEntity('carId', [
		'title' => 'Car',
		'color' => 'blue',
		'weight' => 1547
	]);
$client->insertOrUpdateItems($itemsBatch);

// Define users attributes (do this only when users attributes aren't defined in recommender yet)
$client->addUsersAttribute('name', DataApi\DataType::TEXT, 'en', DataApi\MetaType::TITLE);
$client->addUsersAttribute('age', DataApi\DataType::INTEGER);

// Add some users
$usersBatch = (new DataApi\Batch\EntitiesBatch())
	->addEntity('johnId', [
		'name' => 'John Smith',
		'age' => 35
	])
	->addEntity('sophiaId', [
		'name' => 'Sophia White',
		'age' => 27
	]);
$client->insertOrUpdateUsers($usersBatch);

// Add interactions between users and items
$interactionsBatch = (new DataApi\Batch\InteractionsBatch())
	->addInteraction('johnId', 'carId', 'Like')
	->addInteraction('johnId', 'carId', 'Purchase')
	->addInteraction('johnId', 'fridgeId', 'Dislike')
	->addInteraction('sophiaId', 'carId', 'Detail view')
	->addInteraction('sophiaId', 'fridgeId', 'Purchase');
$client->insertInteractions($interactionsBatch);

// And finally obtain ten recommendations for Sophia and car item!
$recommendations = $client->getRecommendations('sophiaId', 'carId', 10);

// If you want to send more recommendation requests at once, you can use recommendations batch
$batchRecommendations = $client->getRecommendationsBatch((new DataApi\Batch\RecommendationsBatch())
	->requestRecommendations('req1', 10, 'johnId', 'fridgeId', 20)
	->requestRecommendationsForUser('req2', 5, 'sophiaId', 15)
	->requestRecommendationsForItem('req3', 1, 'fridgeId', 10)
);

数据破坏者 – 我们是您的数据感知