public function testCreateRequestUnsetsGuzzleInternals() { $body = array(AbstractCommand::HEADERS_OPTION => 'mock.HEADERS_OPTION', AbstractCommand::ON_COMPLETE => 'mock.ON_COMPLETE', AbstractCommand::DISABLE_VALIDATION => 'mock.DISABLE_VALIDATION', AbstractCommand::RESPONSE_PROCESSING => 'mock.RESPONSE_PROCESSING', AbstractCommand::RESPONSE_BODY => 'mock.RESPONSE_BODY'); $client = new PredictionIOClient(); $request = $client->createRequest(RequestInterface::GET, null, null, $body); $this->assertNull($request->getQuery()->get(AbstractCommand::HEADERS_OPTION)); $this->assertNull($request->getQuery()->get(AbstractCommand::ON_COMPLETE)); $this->assertNull($request->getQuery()->get(AbstractCommand::DISABLE_VALIDATION)); $this->assertNull($request->getQuery()->get(AbstractCommand::RESPONSE_PROCESSING)); $this->assertNull($request->getQuery()->get(AbstractCommand::RESPONSE_BODY)); }
if (!isset($argv[1]) || !isset($argv[2])) { print "Usage: {$argv['0']} <appkey> <data file>\n"; exit(1); } $appkey = $argv[1]; $input = $argv[2]; if (!file_exists($input)) { print "{$input} not found!\n"; exit(1); } if (!is_readable($input)) { print "{$input} cannot be read!\n"; exit(1); } // Instantiate a client $client = PredictionIOClient::factory(array("appkey" => $appkey)); // Scan sample data file and import ratings $handle = fopen($input, "r"); while (!feof($handle)) { $line = fgets($handle); $tuple = explode("\t", $line); if (count($tuple) == 3) { try { $client->identify($tuple[0]); $client->execute($client->getCommand("record_action_on_item", array("pio_action" => "rate", "pio_iid" => $tuple[1], "pio_rate" => intval($tuple[2])))); } catch (Guzzle\Http\Exception\ClientErrorResponseException $e) { print $e->getResponse()->getBody() . "\n\n"; } } } fclose($handle);
public function setupClient(Model $model, $client = null) { $this->_client = $client === null ? \PredictionIO\PredictionIOClient::factory(array('appkey' => Configure::read('predictionIO.appkey'), 'apiurl' => Configure::read('predictionIO.apiurl'))) : $client; }
<?php // web/index.php require_once __DIR__ . '/../vendor/autoload.php'; use PredictionIO\PredictionIOClient; use Symfony\Component\HttpFoundation\Request; $client = PredictionIOClient::factory(['apiurl' => 'http://192.168.33.20:8000', 'appkey' => 'fXcxZlrBYZUxzd6wgeZhIQruai8OfUuUDaQGQyZVeigdfn4gQv48A3Q4Dml5Jfpq']); $app = new Silex\Application(); $app['debug'] = true; $app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/../views')); $app->get('/', function () use($app) { return $app['twig']->render('index.html.twig', []); }); $app->post('/user', function (Request $request) use($app, $client) { $command = $client->getCommand('create_user', ['pio_uid' => $request->get('user')]); $response = $client->execute($command); return $app->json(['message' => sprintf('Created user "%s".', $request->get('user'))]); }); $app->post('/show', function (Request $request) use($app, $client) { $command = $client->getCommand('create_item', ['pio_iid' => $request->get('show'), 'pio_itypes' => 1]); $response = $client->execute($command); $client->identify($request->get('user')); $command = $client->getCommand('record_action_on_item', ['pio_action' => 'like', 'pio_iid' => $request->get('show')]); $client->execute($command); return $app->json(['message' => sprintf('You liked %s', $request->get('show'))]); }); $app->post('/recommend', function (Request $request) use($app, $client) { try { $client->identify($request->get('user')); $command = $client->getCommand('itemrec_get_top_n', ['pio_engine' => 'itemrec', 'pio_n' => 5]); $rec = $client->execute($command);
/** * Returns the items similar to the given item according to the engine. * * @param $itemId * @param $engine * @param int $count * @return mixed */ public function getSimilarItems($itemId, $engine, $count = 3) { $command = $this->client->getCommand('itemsim_get_top_n', array('pio_iid' => $itemId, 'pio_engine' => $engine, 'pio_n' => $count)); $response = $this->client->execute($command); return $response['pio_iids']; }