如何使用PHP从Shoutcast服务器返回最近播放的歌曲

问题描述 投票:0回答:1

我正在尝试使用PHP和JQuery从Shoutcast服务器获取歌曲历史。我已经对一个php文件执行了ajax调用,该文件包含从流媒体服务器获取最近播放的歌曲的代码.Jquery写在我的应用程序文件夹中

jQuery的:

$.ajax({
        type: "GET",
        url: "http://example.com/recent_songs.php",
        headers: {'Access-Control-Allow-Origin': '*'},
        success: function(response){
            console.log(response);
            return false;
        },
        error: function(error){
            console.log(error);
            console.log("Network Error!");
        }
    });

PHP文件驻留在与Jquery文件不同的服务器上。

PHP(recent_songs.php):

$handle = '';
$handle = get_data();

function get_data() {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_USERAGENT,  'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0' );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
        curl_setopt($ch, CURLOPT_TIMEOUT, 8);

        $data_string = "";
        function write_function($handle, $data) {
            global $data_string;
            $data_string .= $data;
            if (strlen($data_string) > 200) {
              return 0;
            }
            else {
              return strlen($data);
             }
         }
        curl_setopt($ch, CURLOPT_URL, 'ip:port/played.html');
        $data = curl_exec($ch);         
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);          
        if ($code!='200'){
            return 'error';
        }else{
            return 'success';           
}

它总是从shoutcast服务器返回http代码0和空响应。但是,当我在浏览器中运行此URL时:ip:port/played.html它显示完整的歌曲历史记录。是否有任何想法从shoutcast服务器获得响应。

任何形式的帮助将不胜感激。谢谢!

php jquery shoutcast
1个回答
1
投票
/* Frisky Radio */
$ip='50.7.64.226';
$port=8000;

function get_data( $ip=false, $port=false ) {
    try{
        if( $ip && $port ){
            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL, "{$ip}:{$port}/played.html" );
            curl_setopt($ch, CURLOPT_USERAGENT,  'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0' );
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
            curl_setopt($ch, CURLOPT_TIMEOUT, 8);

            $data=(object)array(
                'response'  =>  curl_exec($ch),
                'info'      =>  (object)curl_getinfo($ch)
            );

            return $data;
        }

        throw new Exception('Ip address and port number are required');
    }catch( Exception $e ){
        echo $e->getMessage();
    }
}


$data=get_data( $ip, $port );
if( $data->info->http_code==200 ){
    echo '<pre>',print_r( $data->response,true ),'</pre>';
}

以上(稍加编辑)的输出是:

02:09:28    FRISKY | RPO Records Session - March 2017 - Oscar Vasquez   Current Song
01:09:29    FRISKY | Rogue - July 2017 - Amber Long
00:09:32    FRISKY | Feelin FRISKY - June 2017 - Strachan
22:06:31    FRISKY | Voyager - July 2017 - Deepsense
20:02:07    FRISKY | Fatalist - July 2017 - Digital Department
18:00:47    FRISKY | 6th Auditorium - July 2017 - Andrea Cassino
17:02:07    FRISKY | Perspectives - July 2017 - Paul Kardos
16:02:11    FRISKY | Perspectives - July 2017 - Darin Epsilon
15:06:33    FRISKY | Hazendonk FM - July 2017 - Paul Hazendonk
14:03:26    FRISKY | Southside - July 2017 - Graziano Raffa

或者,使用响应数据,您可以使用DOMDocumentDOMXPath来获取显示数据的表格并在某处使用页面。所以,随着回应:

/* Not all shoutcast servers have a list of played songs */
$table='No results';

$dom=new DOMDocument();
$dom->loadHTML( $data->response );

$xp=new DOMXPath( $dom );
$col=$xp->query('//table[2]');


if( !empty( $col ) ){
    $doc=new DOMDocument();
    $doc->appendChild( $doc->importNode( $col->item( 0 ),true ) );
    $table=$doc->saveHTML();
}

$dom = $doc = $xp = $col = null;

echo $table;
© www.soinside.com 2019 - 2024. All rights reserved.