-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpredict.php
More file actions
62 lines (47 loc) · 1.51 KB
/
Copy pathpredict.php
File metadata and controls
62 lines (47 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
// Example using Retail php client libraries to get prediction results
require 'vendor/autoload.php';
use Google\Cloud\Retail\V2\PredictionServiceClient;
use Google\Cloud\Retail\V2\PredictResponse;
use Google\Cloud\Retail\V2\Product;
use Google\Cloud\Retail\V2\ProductDetail;
use Google\Cloud\Retail\V2\UserEvent;
$predictionClient = new PredictionServiceClient([
'credentials' => '[your service account key.json file]'
]);
$placement = "projects/[YOUR PROJECT #]/locations/global/catalogs/default_catalog/placements/product_detail";
$product_detail = new ProductDetail([
'product' => new Product(['id' => '12345']),
'quantity' => 1
]);
$userEvent = new UserEvent([
'event_type' => 'detail-page-view',
'visitor_id' => 'ABCDEFG',
'product_details' => [
new ProductDetail([
'product' => new Product(['id' => '12345']),
'quantity' => 1
])
]
]);
$pb_true = new Google\Protobuf\Value();
$pb_true->setBoolValue(true);
try {
$predictions = $predictionClient->predict($placement, $userEvent, [
'params' => ['returnProduct' => $pb_true],
'filter' => 'filterOutOfStockItems'
]
);
} finally {
$predictionClient->close();
}
$results = $predictions->getResults();
print($results->count() . " Item Id's Returned:");
$iterator = $results->getIterator();
while($iterator->valid()) {
print($iterator->current()->getId() . "\n");
// With returnProduct=true we can get all the product data
print_r(json_decode($iterator->current()->serializeToJsonString()));
$iterator->next();
}
?>