dss89001 2014-08-25 20:17
浏览 46
已采纳

刷新歌曲标题而不重新加载整个页面

I'm building a Shoutcast audio player with the information of the song/artist being played. I have two separate variables ($song and $artist) and their values are updated every time the song changes.

Here is an extract of the code:

<?php

$ip = "111.111.111";
$port = "9436";

$fp = @fsockopen($ip,$port,$errno,$errstr,1);
if (!$fp) 
    { 
    echo "Streaming offline momentaneamente"; // Displays when sever is offline
    } 
    else
    { 
    fputs($fp, "GET /7.html HTTP/1.0
User-Agent: Mozilla

");
    while (!feof($fp)) 
        {
        $info = fgets($fp);
        }
    $info = str_replace('</body></html>', "", $info);
    $split = explode(',', $info);
    if (empty($split[6]) )
        {
        echo "Streaming offline"; // Displays when sever is online but no song title
        }
    else
        {
        $title = str_replace('\'', '`', $split[6]);
        $title = str_replace(',', ' ', $title);
        $split = explode(' - ', $title);
        $artist = trim($split[0]);
        $song = trim($split[1]);
        }


    }

function truncate($string, $length, $dots = "...") {
    return (strlen($string) > $length) ? substr($string, 0, $length - strlen($dots)) . $dots : $string;
}

?>

<div class="player-item-ref acdc">
    <a class="player-link" href="#">
        <h2>AC/DC</h2>
        <ul><!-- THIS UL CONTAINS THE ITEMS TO BE REFRESHED -->
            <li class="song-title"><?php echo truncate("$song", 35); ?></li>
            <li class="song-artist"><?php echo truncate("$artist", 38); ?></li>
        </ul>
    </a>
</div><!-- /.player-container -->

I tried a 5 seconds refresh in JavaScript but it affects the whole page (including the player). I would to make a function which refresh these variables without reloading the whole page.

  • 写回答

1条回答 默认 最新

  • douhuzhi0907 2014-08-25 20:39
    关注

    All you need is in your question's tags: JavaScript, PHP and AJAX.

    You need this things:

    1. create an endpoint in PHP, which returns just song/artist information, for example formatted in JSON or XML
    2. put JavaScript code in your page with the player, which periodically (for example once in 1 or 5 seconds) calls endpoint from point 1 (using for example JavaScript function XMLHttpRequest) and replace part of DOM, which contains song/artist information (probably DIV/SPAN with some ID)

    That's all. That two things together are called AJAX. That abbreviation originally means Asynchronous JavaScript and XML, but you can use also JSON or plain HTML as response from your endpoint, not just XML. AJAX isn't a real thing, it's just a nickname for technique of asynchronous calls in JavaScript.

    In a dumbest way: imagine you have this in your HTML page with a player:

    <div>Now playing: <span id="now_playing"></span></div>

    Create a JavaScript code, which will be executed each 1 second, it will call PHP script you have posted in your question, which returns currently played song as text without any other content (just "Artist - Song") and JavaScript code will put it (overwrite previous content) into span#now_playing. That's all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 如何用visual studio code实现html页面
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?