forked from engineerOfLies/rabbitmqphp_example
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfollow_artists_handler.php
More file actions
62 lines (54 loc) · 2.15 KB
/
Copy pathfollow_artists_handler.php
File metadata and controls
62 lines (54 loc) · 2.15 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
#!/usr/bin/php
<?php
require_once('path.inc');
require_once('get_host_info.inc');
require_once('rabbitMQLib.inc');
require_once('mysqlconnect.php');
function requestProcessor($request) {
if (!isset($request['type'])) {
return ["status" => "error", "message" => "Invalid request type"];
}
switch ($request['type']) {
case 'search':
$query = urlencode($request['query']);
$url = "https://musicbrainz.org/ws/2/artist?query=$query&fmt=json&limit=5";
$response = file_get_contents($url, false, stream_context_create([
"http" => ["header" => "User-Agent: YourAppName/1.0\r\n"]
]));
$data = json_decode($response, true);
$artists = array_map(fn($artist) => [
"id" => $artist['id'],
"name" => $artist['name'],
"disambiguation" => $artist['disambiguation'] ?? ''
], $data['artists'] ?? []);
return ["status" => "success", "artists" => $artists];
case 'follow':
$username = $request['username'];
$artist_id = $request['artist_id'];
$artist_name = $request['artist_name'];
$db = getDB();
try {
$stmt = $db->prepare("
INSERT INTO user_follows (user_id, entity_id, entity_type, name, follow_date)
VALUES (
(SELECT id FROM Users WHERE username = ?), ?, 'artist', ?, NOW()
)
");
$stmt->bind_param("sss", $username, $artist_id, $artist_name);
$stmt->execute();
if ($stmt->affected_rows > 0) {
return ["status" => "success", "message" => "Artist followed"];
} else {
return ["status" => "fail", "message" => "Error following artist or already followed"];
}
} catch (mysqli_sql_exception $e) {
error_log("Database error: " . $e->getMessage());
return ["status" => "error", "message" => "Database error occurred"];
}
default:
return ["status" => "error", "message" => "Unknown request type"];
}
}
$server = new rabbitMQServer("testRabbitMQ.ini", "followMQ");
$server->process_requests('requestProcessor');
?>