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

How to return recently played song from Shoutcast server using PHP

我正在尝试使用 PHP 和 JQuery 从 Shoutcast 服务器获取歌曲历史记录。我已经对 php 文件执行了 ajax 调用,该文件包含用于从流媒体中获取最近播放的歌曲的代码 server.The 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 服务器获得响应。

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

/* 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 获取显示数据的 table 并在页面内某处使用。 因此,响应:

/* 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;