aboutsummaryrefslogtreecommitdiffstats
path: root/lastfm-proxy.php
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lastfm-proxy.php35
1 files changed, 35 insertions, 0 deletions
diff --git a/lastfm-proxy.php b/lastfm-proxy.php
new file mode 100644
index 0000000..7a0a208
--- /dev/null
+++ b/lastfm-proxy.php
@@ -0,0 +1,35 @@
+<?php
+require_once __DIR__ . '/partials/proxy_helpers.php';
+
+header('Content-Type: application/json');
+header('Cache-Control: public, max-age=60');
+
+if (!proxy_rate_limit('lastfm', 60, 60)) {
+ http_response_code(429);
+ echo json_encode(['error' => 'rate limited']);
+ exit;
+}
+
+$api_key = lastfm_api_key();
+$user = 'SillyLaird';
+$url = "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user={$user}&api_key={$api_key}&format=json&limit=5";
+
+$ctx = stream_context_create(['http' => [
+ 'timeout' => 5,
+ 'header' => 'User-Agent: www.sillylaird.ca/1.0',
+]]);
+
+$response = @file_get_contents($url, false, $ctx);
+
+if ($response === false) {
+ http_response_code(502);
+ echo json_encode(['error' => 'Failed to fetch from Last.fm']);
+ exit;
+}
+
+// Cap response size we relay back (defensive — Last.fm responses are tiny).
+if (strlen($response) > 256 * 1024) {
+ $response = substr($response, 0, 256 * 1024);
+}
+
+echo $response;