Skip to content

Instantly share code, notes, and snippets.

@Nathan-Srivi
Created September 25, 2014 10:31
Show Gist options
  • Save Nathan-Srivi/a748f359276662a83644 to your computer and use it in GitHub Desktop.
Save Nathan-Srivi/a748f359276662a83644 to your computer and use it in GitHub Desktop.
Prediction io PHP
<?php session_start();
error_reporting(1) ;
// use composer's autoloader to load PredictionIO PHP SDK
require_once("vendor/autoload.php");
use PredictionIO\PredictionIOClient;
$client = PredictionIOClient::factory(array("appkey" => "mJmKNgP7xnzq7xGBkSRdwk1aqfVL96Q3z3g0GVWIKaxfgafCrmWWunP7iVESCDKy"));
//$client = Flight::prediction_client();
for($i=1;$i<10;$i++)
{
$command = $client->getCommand('create_user', array('pio_uid' => $i));
$response = $client->execute($command);
print_r($response);
}
?>
<?php session_start();
error_reporting(1) ;
// use composer's autoloader to load PredictionIO PHP SDK
require_once("vendor/autoload.php");
use PredictionIO\PredictionIOClient;
$client = PredictionIOClient::factory(array("appkey" => "mJmKNgP7xnzq7xGBkSRdwk1aqfVL96Q3z3g0GVWIKaxfgafCrmWWunP7iVESCDKy"));
$index = 0;
$movies_url = 'https://api.themoviedb.org/3/movie/popular?api_key=MY_API_KEY&page=1' ;
$movies_url_new = file_get_contents($movies_url);
$movies_result = json_decode($movies_url_new, true);
$movies = $movies_result['results'];
$k=1;
foreach($movies as $row){
$id = $row['id'];
$id_ses[] = $k;
$title = $row['title'];
$poster_path = '';
if(!empty($row['poster_path'])){
$poster_path = $row['poster_path'];
}
$moviedetails_url = 'https://api.themoviedb.org/3/movie/' . $id . '?api_key=MY_API_KEY';
$movies_url_new = file_get_contents($moviedetails_url);
$movie = json_decode($movies_url_new, true);
$overview = $movie['overview'];
$release_date = $movie['release_date'];
$command = $client->getCommand('create_item', array('pio_iid' =>$k, 'pio_itypes' => 1));
$command->set('tmdb_id', $id);
$command->set('title', $title);
$command->set('poster_path', $poster_path);
$command->set('overview', $overview);
$command->set('release_date', $release_date);
$client_response = $client->execute($command);
print_r($client_response);
echo "<br><br>";
$index++;
$k++;
}
$_SESSION['m_id'] = $id_ses;
<?php session_start();
error_reporting(1) ;
// use composer's autoloader to load PredictionIO PHP SDK
require_once("vendor/autoload.php");
use PredictionIO\PredictionIOClient;
$client = PredictionIOClient::factory(array("appkey" => "mJmKNgP7xnzq7xGBkSRdwk1aqfVL96Q3z3g0GVWIKaxfgafCrmWWunP7iVESCDKy"));
//print_r($_SESSION['m_id']);
$product = $_SESSION['m_id'];
// each user randomly views 10 items
$s = 1;
for ($u=1; $u<=10; $u++) {
//for ($count=0; $count<10; $count++) {
$i = rand(1, 15); // randomly pick an item
//$i = $product[$s];
echo "User ". $u . " views item ". $i ."<br>";
$client->identify($u);
$client->execute($client->getCommand('record_action_on_item', array('pio_action' => 'view', 'pio_iid' => $i)));
// }
$s++;
}
<?php session_start();
require_once("vendor/autoload.php");
use PredictionIO\PredictionIOClient;
$client = PredictionIOClient::factory(array("appkey" => "6DzhlG31iEHRo1ThUeIswYoXBHYq825Xg8d8ZooUymdXVLBOvbWRBCyCIhfLEAhs"));
try{
$user_id = 6;
$client->identify($user_id);
$command = $client->getCommand('itemrec_get_top_n', array('pio_engine' => 'movie-recommender', 'pio_n' => 9));
$recommended_movies_raw = $client->execute($command);
echo "<pre>";
print_r( $recommended_movies_raw);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment