2010-08-15 13 views
7

मुझे PHP के साथ कुछ परेशानी देखने वाले आंकड़े (दर्शक, वर्तमान गीत खेल आदि) हैं और मुझे कोई जानकारी नहीं मिल सकती है कि यह कैसे करें।आईएसकास्ट 2 आंकड़े दिखाने के लिए PHP का उपयोग करें

आईएसकास्ट 2 के साथ कुछ एक्सएलएस फाइलें शामिल हैं और मैं इन फ़ाइलों को अपनी साइट पर PHP के साथ शामिल कर सकता हूं लेकिन मैं DIV को अपडेट नहीं करना चाहता हूं जिसमें शामिल प्रत्येक 5 सेकंड में है और यह एक्सएलएस के साथ काम नहीं करेगा फ़ाइलें।

धन्यवाद!

उत्तर

14

नमस्कार और कोड के लिए धन्यवाद। मैंने इससे कक्षा बनाई और कुछ चेक जोड़े ताकि सर्वर ऑफलाइन होने पर शिकायत न हो। जब से मैं इसे यहाँ से ले लिया मैं कक्षा वापस साझा करेंगे:

<?php 

class IceCast { 
    var $server = "http://localhost:8000"; 
    var $stats_file = "/status.xsl"; 
    var $radio_info=array(); 

    function __construct() { 
     //build array to store our radio stats for later use   
     $this->radio_info['server'] = $this->server; 
     $this->radio_info['title'] = 'Offline'; 
     $this->radio_info['description'] = 'Radio offline'; 
     $this->radio_info['content_type'] = ''; 
     $this->radio_info['mount_start'] = ''; 
     $this->radio_info['bit_rate'] = ''; 
     $this->radio_info['listeners'] = ''; 
     $this->radio_info['most_listeners'] = ''; 
     $this->radio_info['genre'] = ''; 
     $this->radio_info['url'] = ''; 
     $this->radio_info['now_playing'] = array(); 
     $this->radio_info['now_playing']['artist'] = 'Unknown'; 
     $this->radio_info['now_playing']['track'] = 'Unknown'; 
    } 

    function setUrl($url) { 
     $this->server=$url; 
     $this->radio_info['server'] = $this->server; 
    } 

    private function fetch() { 
     //create a new curl resource 
     $ch = curl_init(); 

     //set url 
     curl_setopt($ch,CURLOPT_URL,$this->server.$this->stats_file); 

     //return as a string 
     curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 

     //$output = our stauts.xsl file 
     $output = curl_exec($ch); 

     //close curl resource to free up system resources 
     curl_close($ch); 

     return $output; 
    } 

    function getStatus() { 
     $output=$this->fetch(); 

     //loop through $ouput and sort into our different arrays 
     $temp_array = array(); 

     $search_for = "<td\s[^>]*class=\"streamdata\">(.*)<\/td>"; 
     $search_td = array('<td class="streamdata">','</td>'); 


     if(preg_match_all("/$search_for/siU",$output,$matches)) { 
      foreach($matches[0] as $match) { 
       $to_push = str_replace($search_td,'',$match); 
       $to_push = trim($to_push); 
       array_push($temp_array,$to_push); 
      } 
     } 

     if(count($temp_array)) { 
      //sort our temp array into our ral array 
      $this->radio_info['title'] = $temp_array[0]; 
      $this->radio_info['description'] = $temp_array[1]; 
      $this->radio_info['content_type'] = $temp_array[2]; 
      $this->radio_info['mount_start'] = $temp_array[3]; 
      $this->radio_info['bit_rate'] = $temp_array[4]; 
      $this->radio_info['listeners'] = $temp_array[5]; 
      $this->radio_info['most_listeners'] = $temp_array[6]; 
      $this->radio_info['genre'] = $temp_array[7]; 
      $this->radio_info['url'] = $temp_array[8]; 

      if(isset($temp_array[9])) { 
       $x = explode(" - ",$temp_array[9]); 
       $this->radio_info['now_playing']['artist'] = $x[0]; 
       $this->radio_info['now_playing']['track'] = $x[1]; 
      } 
     } 
     return $this->radio_info; 
     } 

} 
?> 
+0

बहुत उपयोगी! धन्यवाद –

+1

यह एक बहुत बुरा विचार है और यह आइसकास्ट संस्करण 1.4 के साथ टूट जाएगा, क्योंकि इसमें एक पुनः निर्मित वेबइंटरफेस है। – ePirat

6

इस कोड को मैं काम कर रहा यह मिल गया है का उपयोग करके:

<?php 

/* 
* SCRIPT CONFIGURATIONS 
*/ 
$SERVER = 'http://myserver.com:8000'; //URL TO YOUR ICECAST SERVER 
$STATS_FILE = '/status.xsl'; //PATH TO STATUS.XSL PAGE YOU CAN SEE IN YOUR BROWSER (LEAVE BLANK UNLESS DIFFERENT) 

///////////////////// END OF CONFIGURATION --- DO NOT EDIT BELOW THIS LINE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 

//create a new curl resource 
$ch = curl_init(); 

//set url 
curl_setopt($ch,CURLOPT_URL,$SERVER.$STATS_FILE); 

//return as a string 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 

//$output = our stauts.xsl file 
$output = curl_exec($ch); 

//close curl resource to free up system resources 
curl_close($ch); 

//build array to store our radio stats for later use 
$radio_info = array(); 
$radio_info['server'] = $SERVER; 
$radio_info['title'] = ''; 
$radio_info['description'] = ''; 
$radio_info['content_type'] = ''; 
$radio_info['mount_start'] = ''; 
$radio_info['bit_rate'] = ''; 
$radio_info['listeners'] = ''; 
$radio_info['most_listeners'] = ''; 
$radio_info['genre'] = ''; 
$radio_info['url'] = ''; 
$radio_info['now_playing'] = array(); 
    $radio_info['now_playing']['artist'] = ''; 
    $radio_info['now_playing']['track'] = ''; 

//loop through $ouput and sort into our different arrays 
$temp_array = array(); 

$search_for = "<td\s[^>]*class=\"streamdata\">(.*)<\/td>"; 
$search_td = array('<td class="streamdata">','</td>'); 

if(preg_match_all("/$search_for/siU",$output,$matches)) { 
    foreach($matches[0] as $match) { 
     $to_push = str_replace($search_td,'',$match); 
     $to_push = trim($to_push); 
     array_push($temp_array,$to_push); 
    } 
} 

//sort our temp array into our ral array 
$radio_info['title'] = $temp_array[0]; 
$radio_info['description'] = $temp_array[1]; 
$radio_info['content_type'] = $temp_array[2]; 
$radio_info['mount_start'] = $temp_array[3]; 
$radio_info['bit_rate'] = $temp_array[4]; 
$radio_info['listeners'] = $temp_array[5]; 
$radio_info['most_listeners'] = $temp_array[6]; 
$radio_info['genre'] = $temp_array[7]; 
$radio_info['url'] = $temp_array[8]; 

$x = explode(" - ",$temp_array[9]); 
$radio_info['now_playing']['artist'] = $x[0]; 
$radio_info['now_playing']['track'] = $x[1]; 

?> 
2

कृपया मेरा उत्तर over here पर एक नज़र डालें कारण है कि यह एक बहुत ही बुरा विचार इस स्क्रिप्ट का उपयोग करने के लिए है।
टीएल; डॉ: आपको Icecast HTML स्थिति पृष्ठ को पार्स नहीं करना चाहिए।