forked from engineerOfLies/rabbitmqphp_example
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrecommendations_handler.php
More file actions
90 lines (69 loc) · 2.4 KB
/
recommendations_handler.php
File metadata and controls
90 lines (69 loc) · 2.4 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/php
<?php
require_once('path.inc');
require_once('get_host_info.inc');
require_once('rabbitMQLib.inc');
require_once('mysqlconnect.php');
$env = parse_ini_file('.env');
$apiKey = $env['HASDATA_API'];
$mydb = getDB();
function getRecommendations($username)
{
global $mydb, $apiKey;
$stmt = $mydb->prepare("
SELECT Events.venue_name
FROM Events
JOIN User_Likes ON Events.event_id = User_Likes.event_id
JOIN Users ON User_Likes.id = Users.id
WHERE Users.username = ?
LIMIT 1
");
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
$likedEvent = $result->fetch_assoc();
if (!$likedEvent) {
return ["status" => "success", "recommendations" => []];
}
$venueName = $likedEvent['venue_name'];
error_log("Fetching recommendations for venue: $venueName");
$url = "https://api.hasdata.com/scrape/google/events?q=" . urlencode($venueName);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["x-api-key: $apiKey"]);
$response = curl_exec($ch);
curl_close($ch);
error_log("API Response: " . $response);
$apiEvents = json_decode($response, true);
if (!isset($apiEvents['eventsResults']) || empty($apiEvents['eventsResults'])) {
error_log("No events found in API response");
return ["status" => "success", "recommendations" => []];
}
$recommendations = [];
foreach ($apiEvents['eventsResults'] as $event) {
$recommendations[] = [
'title' => $event['title'],
'date' => $event['date']['when'] ?? '',
'address' => implode(", ", $event['address'] ?? []),
'thumbnail' => $event['thumbnail'] ?? '',
'link' => $event['link'] ?? '',
'description' => $event['description'] ?? ''
];
}
error_log("Recommendations: " . json_encode($recommendations));
return ["status" => "success", "recommendations" => $recommendations];
}
function requestProcessor($request)
{
if (!isset($request['type']) || $request['type'] !== 'get_recommendations') {
return ["status" => "fail", "message" => "Invalid request type"];
}
if (!isset($request['username'])) {
return ["status" => "fail", "message" => "Username not provided"];
}
$username = $request['username'];
return getRecommendations($username);
}
$server = new rabbitMQServer("testRabbitMQ.ini", "recommendationsMQ");
$server->process_requests('requestProcessor');
?>