Example #1
0
const SERVICE_ACCOUNT_NAME = 'INSERT_YOUR_SERVICE_ACCOUNT_NAME';
// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = '/super/secret/path/to/key.p12';
$client = new Google_Client();
$client->setApplicationName("Google Prediction Sample");
// Set your cached access token. Remember to replace $_SESSION with a
// real database or memcached.
session_start();
if (isset($_SESSION['token'])) {
    $client->setAccessToken($_SESSION['token']);
}
// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(SERVICE_ACCOUNT_NAME, array('https://www.googleapis.com/auth/prediction'), $key));
$client->setClientId(CLIENT_ID);
$service = new Google_PredictionService($client);
// Prediction logic:
$id = 'sample.languageid';
$predictionData = new Google_InputInput();
$predictionData->setCsvInstance(array('Je suis fatigue'));
$input = new Google_Input();
$input->setInput($predictionData);
$result = $service->hostedmodels->predict($id, $input);
print '<h2>Prediction Result:</h2><pre>' . print_r($result, true) . '</pre>';
// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
if ($client->getAccessToken()) {
    $_SESSION['token'] = $client->getAccessToken();
}
<body>
<header><h1>Google Prediction API Sample App (PHP)</h1></header>
<div class="box">

<!--<div>Status: <?php 
print $status;
?>
</div>-->

<?php 
if (isset($authUrl)) {
    print "<a class='login' href='{$authUrl}'>Login</a>";
    $result = "";
    print "</div>";
} else {
    print "<a class='login' href='?logout'>Logout</a>";
    /* prediction logic follows...  */
    $id = "languages";
    $predictionText = "Je suis fatigue";
    $predictionData = new Google_InputInput();
    $predictionData->setCsvInstance(array($predictionText));
    $input = new Google_Input();
    $input->setInput($predictionData);
    $result = $predictionService->trainedmodels->predict($id, $input);
    print "</div><br><br><h2>Prediction Result:</h2>";
    print_r($result);
}
?>
</body>
</html>