0

i have little problem with XML. My XML file is look like this:

http://pastebin.com/MQUB2W2B i user pastebin because this site don't support xml code.. ://

so i need to select source mount "/stream" and other source mount "/". And my PHP code look like this:

Class stream {

    Public Function __construct($host, $path, $user, $pass, $mpoint){

        $this->host = $host;
        $this->path = $path;
        $this->user = $user;
        $this->password = $pass;
        $this->mpoint = $mpoint;

        $this->url = "http://{$this->user}:{$this->password}@{$this->host}/{$this->path}";
        $this->xml = simplexml_load_file($this->url);

    }

    Public Function audio($url, $format){

        return print "<audio controls> <source src=\"{$url}\" type=\"audio/{$format}\"> </audio>";

    }

    Public Function stream_meta(){

        $xml = $this->xml->registerXPathNamespace('test', '/{$this->mpoint}');
        $result = $xml->xpath('test:artist');

        return $result;

    }

    Public Function nowplay(){

        return $this->xml->source[0]->artist;
    }

    Public Function download($format){

        $link = "http://{$this->host}/{$this->mpoint}";

        if($format == "m3u"){
            return "{$link}.m3u";
        }

        elseif($format == "xspf"){
            return "{$link}.xspf";
        }

        elseif($format == "vclt"){
            return "{$link}.vclt";
        }

        return false;

    }


}

So now i asking what i do wrong or what i can do better? i need to select artist data and know what is the source mount "?what is this?". I really need help. I don't know what i can do and i don't invent other way to do this. I am so tired and i am really need help! Please, can anyone help me? I don't want to use "nowplay()" function. because source mount "/example" change sometimes...

T0niiiiii
  • 5
  • 3

1 Answers1

0

I'm not entirely clear on what you are trying to achieve, but here is a basic example of getting the mount and artist of each source in the XML:

function sources(){
    foreach($this->xml->source as $source) {
        echo "Mount: " . $source->attributes()['mount'] . '<br>';
        echo "Artist: " . $source->artist . '<br>';
    }
}

Or to find the artist for a source:

$source = $this->find_source('/stream');
$artist = $source->artist;

function find_source($s){
    foreach($this->xml->source as $source) {
        if ($source->attributes()['mount'] == $s) {
            return $source;
        }
    }
}

Alternatively, you could do the same with XPath without needing to use a loop. For example:

function find_source($s){
    return $this->xml->xpath('/icestats/source[@mount="'.$s.'"]')[0];
}
Community
  • 1
  • 1
John C
  • 8,223
  • 2
  • 36
  • 47