2017 © Pedro Peláez
 

library php-api-client

PHP client for easy use of the Recombee recommendation API.

image

recombee/php-api-client

PHP client for easy use of the Recombee recommendation API.

  • Tuesday, May 15, 2018
  • by recombee
  • Repository
  • 1 Watchers
  • 12 Stars
  • 15,876 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 6 Forks
  • 0 Open issues
  • 21 Versions
  • 10 % Grown

The README.md

Recombee API Client

A PHP client for easy use of the Recombee recommendation API., (*1)

If you don't have an account at Recombee yet, you can create a free account here., (*2)

Documentation of the API can be found at docs.recombee.com., (*3)

Installation

The best way to install the client is through dependency manager Composer:, (*4)

composer require recombee/php-api-client

or, (*5)

{
    "require": {
        "recombee/php-api-client": "^4.1.2"
    }
}

Examples

Basic example

use Recombee\RecommApi\Client;
use Recombee\RecommApi\Requests as Reqs;
use Recombee\RecommApi\Exceptions as Ex;

$client = new Client('--my-database-id--', '--db-private-token--', ['region' => 'us-west']);

const NUM = 100;
const PROBABILITY_PURCHASED = 0.1;

try
{
    // Generate some random purchases of items by users
    $purchase_requests = array();
    for($i=0; $i < NUM; $i++) {
        for($j=0; $j < NUM; $j++) {
            if(mt_rand() / mt_getrandmax() < PROBABILITY_PURCHASED) {

                $request = new Reqs\AddPurchase("user-{$i}", "item-{$j}",
                    ['cascadeCreate' => true] // Use cascadeCreate to create the
                                              // yet non-existing users and items
                );
                array_push($purchase_requests, $request);
            }
        }
    }
    echo "Send purchases\n";
    $res = $client->send(new Reqs\Batch($purchase_requests)); //Use Batch for faster processing of larger data

    // Get 5 recommendations for user 'user-25'
    $response = $client->send(new Reqs\RecommendItemsToUser('user-25', 5));
    echo 'Recommended items: ' . json_encode($response, JSON_PRETTY_PRINT) . "\n";

    // User scrolled down - get next 3 recommended items
    $response = $client->send(new Reqs\RecommendNextItems($response['recommId'], 3));
    echo 'Next recommended items: ' . json_encode($response, JSON_PRETTY_PRINT) . "\n";
}
catch(Ex\ApiException $e)
{
    //use fallback
}

Using property values

use Recombee\RecommApi\Client;
use Recombee\RecommApi\Requests as Reqs;
use Recombee\RecommApi\Exceptions as Ex;

const NUM = 100;
const PROBABILITY_PURCHASED = 0.1;

$client = new Client('--my-database-id--', '--db-private-token--', ['region' => 'ap-se']);
$client->send(new Reqs\ResetDatabase()); // Clear everything from the database

/*
We will use computers as items in this example
Computers have five properties 
  - price (floating point number)
  - number of processor cores (integer number)
  - description (string)
  - date from which it is in stock (timestamp)
  - image (url of computer's photo)
*/

// Add properties of items
$client->send(new Reqs\AddItemProperty('price', 'double'));
$client->send(new Reqs\AddItemProperty('num-cores', 'int'));
$client->send(new Reqs\AddItemProperty('description', 'string'));
$client->send(new Reqs\AddItemProperty('in_stock_from', 'timestamp'));
$client->send(new Reqs\AddItemProperty('image', 'image'));

# Prepare requests for setting a catalog of computers
$requests = array();
for($i=0; $i<NUM; $i++)
{
    $itemId = "computer-{$i}";
    $r = new Reqs\SetItemValues(
      $itemId,
      //values:
      [ 
        'price' => rand(15000, 25000),
        'num-cores' => rand(1, 8),
        'description' => 'Great computer',
        'in_stock_from' => new DateTime('NOW'),
        'image' => "http://examplesite.com/products/{$itemId}.jpg"
      ],
      //optional parameters:
      ['cascadeCreate' => true] // Use cascadeCreate for creating item
                                 // with given itemId, if it doesn't exist]
    );
    array_push($requests, $r);
}

// Send catalog to the recommender system
$result =  $client->send(new Reqs\Batch($requests));
var_dump($result);

// Generate some random purchases of items by users
$requests = array();

for($i=0; $i<NUM; $i++)
    for($j=0; $j<NUM; $j++)
        if(mt_rand() / mt_getrandmax() < PROBABILITY_PURCHASED)
        {
           $r = new Reqs\AddPurchase("user-{$i}", "computer-{$j}", ['cascadeCreate' => true]);
           array_push($requests, $r);
        }

// Send purchases to the recommender system
$client->send(new Reqs\Batch($requests));

// Get 5 items related to item computer-6. Personalize them for user-42, who is currently viewing that item.
// Recommend only computers that have at least 3 cores
$recommended = $client->send(
  new Reqs\RecommendItemsToItem('computer-6', 'user-42', 5, ['filter' => "'num-cores'>=3"])
  );
echo 'Recommended items with at least 3 processor cores: ' . json_encode($recommended, JSON_PRETTY_PRINT) . "\n";

// Recommend only items that are more expensive then currently viewed item computer-6 (up-sell)
$recommended = $client->send(
  new Reqs\RecommendItemsToItem('computer-6', 'user-42', 5,
    ['filter' => "'price' > context_item[\"price\"]"])
  );
echo 'Recommended up-sell items: ' . json_encode($recommended, JSON_PRETTY_PRINT) . "\n";


// Filters, boosters and other settings can be set also in the Admin UI (admin.recombee.com)
// when scenario is specified
$recommended = $client->send(
  new Reqs\RecommendItemsToItem('computer-6', 'user-42', 5, ['scenario' => 'product_detail'])
  );

// Perform personalized full-text search with a user's search query (e.g. 'computers')
$matches = $client->send(
  new Reqs\SearchItems('user-42', 'computers', 5, ['scenario' => 'search_top'])
  );
echo 'Matched items: ' . json_encode($matches, JSON_PRETTY_PRINT) . "\n";

Exception handling

For the sake of brevity, the above examples omit exception handling. However, various exceptions can occur while processing request, for example because of adding an already existing item, submitting interaction of nonexistent user or because of timeout., (*6)

We are doing our best to provide the fastest and most reliable service, but production-level applications must implement a fallback solution since errors can always happen. The fallback might be, for example, showing the most popular items from the current category, or not displaying recommendations at all., (*7)

Example:, (*8)

use Recombee\RecommApi\Client;
use Recombee\RecommApi\Requests as Reqs;
use Recombee\RecommApi\Exceptions as Ex;

try
{
    $recommended = $client->send(
      new Reqs\RecommendItemsToItem('computer-6', 'user-42', 5,
        ['filter' => "'price' > context_item[\"price\"]"])
    );
}
catch(Ex\ApiTimeoutException $e)
{
    //Handle timeout => use fallback
}
catch(Ex\ResponseException $e)
{
    //Handle errorneous request => use fallback
}
catch(Ex\ApiException $e)
{
    //ApiException is parent of both ResponseException and ApiTimeoutException
}

The Versions

15/05 2018

dev-master

9999999-dev https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

15/05 2018

v2.1.1

2.1.1.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

05/04 2018

v2.1.0

2.1.0.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

23/03 2018

v2.0.1

2.0.1.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

19/02 2018

v2.0.0

2.0.0.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

12/02 2018

v1.6.1

1.6.1.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

26/10 2017

v1.6.0

1.6.0.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

18/09 2017

v1.5.0

1.5.0.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

25/08 2017

v1.4.0

1.4.0.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

03/03 2017

v1.3.2

1.3.2.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

27/02 2017

v1.3.1

1.3.1.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

29/12 2016

v1.3

1.3.0.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

20/10 2016

v1.2.5

1.2.5.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

04/10 2016

v1.2.4

1.2.4.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

16/09 2016

v1.2.3.1

1.2.3.1 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

23/08 2016

v1.2.3

1.2.3.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

05/08 2016

v1.2.2

1.2.2.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

05/08 2016

v1.2.1

1.2.1.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

27/06 2016

v1.2

1.2.0.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

20/05 2016

v1.1

1.1.0.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler

16/05 2016

v1.0

1.0.0.0 https://recombee.com

PHP client for easy use of the Recombee recommendation API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ondřej Fiedler