AJAX 每 5 秒更新一次查询,以输出 Icecast 服务器的专辑封面



一些背景:

我在Windows计算机上运行了Icecast 2.4.3。我制作了一个"正在播放"xsl文件,该文件驻留在Icecast的"web"目录中。文件中当前播放的歌曲的示例如下:

getMusic({
    "/stream":{
        "server_name":"Radio",
        "title":"Jack Johnson - Crying Shame"}
    });

在我的网页上,我有一个超时功能,它会自动将艺术家和歌曲标题更改为 id 为"track-title2"。此脚本如下所示:

<script>
function updateTitle() {
    $.ajax({
        type: 'GET',
        url: 'https://radio.domain.com/np.xsl',
        jsonpCallback: 'getMusic',
        dataType: 'jsonp'
    }).then(function (data) {
        var $track = $('#track-title').text(data['/stream'].title);
    }).fail(function (e) {
        console.log(e);
    }).always(function () {
        setTimeout(updateTitle, 5000);
    });
}
$(updateTitle);
</script>

所有的工作...

现在,我想通过访问 LAST 添加专辑封面来加强我的网页。调频接口。此函数位于另一个名为"loadme.php"的独立PHP文件中。

问题:

我似乎无法弄清楚如何解析我的 AJAX GET 请求(在本例中(输出到屏幕杰克约翰逊 - 哭泣的耻辱。我需要抓取"-"之前的所有字符,同样,获取"-"之后的所有字符并将其放入两个 PHP 变量中,即 $artist 和 $title(分别(。

由于艺术家和标题也可能发生变化,我希望此调用类似于上面的脚本。

我的索引中的 PHP 调用.php:

<?php
echo '<img id="albumart" style="text-align:center;" src="';
echo LastFMArtwork::getArtwork('Jack Johnson', 'Crying Shame', true, "small");
echo '">';
?>

我显然已经对标题进行了硬编码,以确保 API 正确......是的,专辑封面确实显示在我的网页上。

请帮助:开发一个JSON AJAX脚本,该脚本每5秒更新一次,调用PHP函数"LastFMArtwork::getArtwork",但可以将艺术家和标题解析为两个PHP变量。任何帮助和/或指导将胜感激。

谢谢。

因此,在Brad的一点指导下(好吧,一堆(,我能够使用SSE(服务器发送事件(和AJAX的混合体。

我的索引.php页:

<!DOCTYPE html>
<html xml:lang="en" lang="en" dir="ltr" prefix="og: http://ogp.me/ns#">
	<head>
		<link rel="icon" href="favicon.ico" type="image/x-icon">
		<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
     <meta name="apple-mobile-web-app-capable" content="yes">
		<link rel="apple-touch-icon" href="apple-touch-icon.png" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
		<meta name="description" content="Person's personal and professional radio station. ***Contains explicit content***">
		<meta name="keywords" content="icecast,radio,internet radio,station,mediamonkey">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=3">
		<meta property="og:url" content="https://radio.domain.com" />
		<meta property="og:image" content="https://radio.domain.com/ogimage.png" />
		<meta property="og:site_name" content="Radio" />
		<meta property="og:description" content="Person's personal internet radio station" />
		<title id="track-title"></title>
		<style>
			html{width:100%;}
			body{background-color:#bfbfbf; text-align:center; font-family:Helvetica;}
			#wrapper{position:absolute; width:300px; height:450px; max-width:550px; max-height:700px; left:50%; transform:translate(-50%,0); -ms-transform:translate(-50%,0); -webkit-transform:translate(-50%,0); margin-right:-50%; text-align:center; box-shadow:1px 1px 20px 5px #4d4d4d;}
                        #albumart{position:relative; text-align:center; box-shadow:1px 1px 20px 5px #4d4d4d;}
			#radio{position: relative;}
		</style>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
		<script>
function updateTitle() {
	$.ajax({
		type: 'GET',
		url: 'https://radio.domain.com/np.xsl',
		jsonpCallback: 'parseMusic',
		dataType: 'jsonp'
	}).then(function (data) {
		var $track = $('#track-title').text(data['/stream'].title);
		var text = $track.text();
		$track.text(text.replace(" - MediaMonkey",""));
	}).fail(function (e) {
		console.log(e);
	}).always(function () {
		setTimeout(updateTitle, 12500);
	});
}
$(updateTitle);
function sendRequest(){
    $.ajax({
	    type: 'GET',
        url: 'https://radio.domain.com/getalbum.php',
        success: function(data){
                document.getElementById("art").innerHTML = data;
		console.log(data);
                setTimeout(function(){
                    sendRequest();
                }, 10000);
        }});
};
$(sendRequest);
</script>
</head>
<body>
        <div id="wrapper">
        <h2>Live Radio</h2>
        <span id="art"></span>
        <audio id="radio" controls src="https://radio.domain.com/stream" type="audio/mp3"></audio>
        </div>
</body>
</html>

从AJAX调用的SSE页面(getalbum.php(:

<?php
require_once("lastfm.php");
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$exe = curl_init();
curl_setopt($exe, CURLOPT_URL, "https://radio.domain.com/np.xsl");
curl_setopt($exe, CURLOPT_HEADER, 0);
curl_setopt($exe, CURLOPT_RETURNTRANSFER, 1);
$raw = curl_exec($exe);
curl_close($exe);
$json = $raw;
//The next 7 lines gets rids of all the "extra stuff" from the np.xsl file
$json = str_replace("n","",$json);
$json = str_replace("t","",$json);
$json = str_replace(""","",$json);
$json = str_replace("({","",$json);
$json = str_replace("nt","",$json);
$json = str_replace("parseMusic/stream:{title:","",$json);
$json = str_replace(" - MediaMonkey}});","",$json);
//Find the dash and minus one is the artist!
$artist = substr($json, 0, strpos($json, "-") - 1);
//Find the dash and add 2 and that is the title!
$title = substr($json, strpos($json, "-") + 2, strlen($json));
echo '<h4>'.$artist.' - '.$title.'</br></br>';
echo '<img id="albumart" title="'.$title.'" style="text-align:center;" width="280" height="280" src="';
echo LastFMArtwork::getArtwork("$artist", "$title", true, "extralarge");
echo '"></br>';
flush();

我的直播网站场景:截图1

最新更新