aboutsummaryrefslogtreecommitdiffstats
path: root/lastfm-proxy.php
blob: 7a0a20806c6b31731681f7a23c06a307cce45e3c (plain) (blame)
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
<?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;