Your SHOUTcast statistics URL normally looks like this:
http://your-server.com:8000/stats?sid=1&json=1Replace the hostname, port and stream ID with your own server details.
<?php
declare(strict_types=1);
$statsUrl = 'http://your-server.com:8000/stats?sid=1&json=1';
$curl = curl_init($statsUrl);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 10,
CURLOPT_USERAGENT => 'StreamCode Studio',
]);
$response = curl_exec($curl);
curl_close($curl);
$stats = is_string($response)
? json_decode($response, true)
: null;
?>You can then display the current listener count:
<?php if (is_array($stats)): ?>
<p>
Current listeners:
<strong>
<?= number_format(
(int)($stats['currentlisteners'] ?? 0)
); ?>
</strong>
</p>
<?php else: ?>
<p>
Listener statistics are currently unavailable.
</p>
<?php endif; ?>Display the Current Song
<?php if (is_array($stats)): ?>
<p>
Now playing:
<strong>
<?= htmlspecialchars(
(string)($stats['songtitle'] ?? 'Unknown'),
ENT_QUOTES | ENT_SUBSTITUTE,
'UTF-8'
); ?>
</strong>
</p>
<?php endif; ?>Complete Example
<?php
declare(strict_types=1);
$statsUrl = 'http://your-server.com:8000/stats?sid=1&json=1';
$curl = curl_init($statsUrl);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 10,
CURLOPT_USERAGENT => 'StreamCode Studio',
]);
$response = curl_exec($curl);
curl_close($curl);
$stats = is_string($response)
? json_decode($response, true)
: null;
$listeners = (int)($stats['currentlisteners'] ?? 0);
$currentSong = (string)($stats['songtitle'] ?? 'Unknown');
?>
<?php if (is_array($stats)): ?>
<div class="shoutcast-stats">
<p>
<strong>
<?= number_format($listeners); ?>
</strong>
<?= $listeners === 1
? 'listener'
: 'listeners'; ?>
</p>
<p>
Now playing:
<strong>
<?= htmlspecialchars(
$currentSong,
ENT_QUOTES | ENT_SUBSTITUTE,
'UTF-8'
); ?>
</strong>
</p>
</div>
<?php else: ?>
<p>
Stream statistics are currently unavailable.
</p>
<?php endif; ?>Change the following line to your own SHOUTcast statistics URL:
$statsUrl = 'http://your-server.com:8000/stats?sid=1&json=1';SHOUTcast 2 normally uses `sid=1`, although stations with multiple streams may use a different stream ID.
Article discussion
Share your thoughts about this article.
Log in or create an account to comment.
No comments yet. Be the first to join the discussion.