簡體   English   中英

用於回顯VLC的PHP腳本現在播放XML屬性

[英]PHP script to echo VLC now playing XML attributes

我一直在尋找這一點,並沒有太多的運氣。 我發現有很多資源顯示如何從動態XML回顯數據,但我是一個PHP新手,我寫的任何東西似乎都沒有抓住並打印出我想要的東西,盡管從我聽過的所有內容來看,它應該是相對容易。 源XML(位於192.168.0.15:8080/requests/status.xml)如下:

<root>
  <fullscreen>0</fullscreen>
  <volume>97</volume>
  <repeat>false</repeat>
  <version>2.0.5 Twoflower</version>
  <random>true</random>
  <audiodelay>0</audiodelay>
  <apiversion>3</apiversion>
  <videoeffects>
  <hue>0</hue>
  <saturation>1</saturation>
  <contrast>1</contrast>
  <brightness>1</brightness>
  <gamma>1</gamma>
  </videoeffects>
  <state>playing</state>
  <loop>true</loop>
  <time>37</time>
  <position>0.22050105035305</position>
  <rate>1</rate>
  <length>168</length>
  <subtitledelay>0</subtitledelay>
  <equalizer/>
  <information>
  <category name="meta">
  <info name="description">
  000003EC 00000253 00000D98 000007C0 00009C57 00004E37 000068EB 00003DC5 00015F90 00011187
  </info>
  <info name="date">2003</info>
  <info name="artwork_url"> file://brentonshp04/music%24/Music/Hackett%2C%20Steve/Guitar%20Noir%20%26%20There%20Are%20Many%20Sides%20to%20the%20Night%20Disc%202/Folder.jpg
  </info>
  <info name="artist">Steve Hackett</info>
  <info name="publisher">Recall</info>
  <info name="album">Guitar Noir &amp; There Are Many Sides to the Night Disc 2
  </info>
  <info name="track_number">5</info>
  <info name="title">Beja Flor [Live]</info>
  <info name="genre">Rock</info>
  <info name="filename">Beja Flor [Live]</info>
  </category>
  <category name="Stream 0">
  <info name="Bitrate">128 kb/s</info>
  <info name="Type">Audio</info>
  <info name="Channels">Stereo</info>
  <info name="Sample rate">44100 Hz</info>
  <info name="Codec">MPEG Audio layer 1/2/3 (mpga)</info>
  </category>
  </information>
  <stats>
  <lostabuffers>0</lostabuffers>
  <readpackets>568</readpackets>
  <lostpictures>0</lostpictures>
  <demuxreadbytes>580544</demuxreadbytes>
  <demuxbitrate>0.015997290611267</demuxbitrate>
  <playedabuffers>0</playedabuffers>
  <demuxcorrupted>0</demuxcorrupted>
  <sendbitrate>0</sendbitrate>
  <sentbytes>0</sentbytes>
  <displayedpictures>0</displayedpictures>
  <demuxreadpackets>0</demuxreadpackets>
  <sentpackets>0</sentpackets>
  <inputbitrate>0.016695899888873</inputbitrate>
  <demuxdiscontinuity>0</demuxdiscontinuity>
  <averagedemuxbitrate>0</averagedemuxbitrate>
  <decodedvideo>0</decodedvideo>
  <averageinputbitrate>0</averageinputbitrate>
  <readbytes>581844</readbytes>
  <decodedaudio>0</decodedaudio>
  </stats>
  </root> 

我想寫的是一個簡單的PHP腳本,它回應了藝術家的名字(在這個例子中Steve Hackett)。 實際上我想要它回應藝術家,歌曲和專輯,但我相信如果我被展示如何找回一個,我可以自己弄清楚剩下的。

我的劇本實際上似乎有用的東西如下。 我嘗試過的不僅僅是下面的內容,但是我遺漏了一些我認識的事實並沒有奏效。

<?PHP
$file = file_get_contents('http://192.168.0.15:8080/requests/status.xml');
$sxe = new SimpleXMLElement($file);

foreach($sxe->...

echo "Artist: "...

?>

我想我需要使用foreachecho ,但我無法弄清楚如何以打印這些信息括號之間的方式來實現它。

如果我遺漏了任何東西,我很抱歉。 我不僅是PHP的新手,但我也是StackOverflow的新手。 我在其他項目中引用了這個網站,它總是非常有用,所以提前感謝您的耐心和幫助!

////////完成的工作腳本 - 感謝Stefano和所有幫助過的人!

<?PHP
$file = file_get_contents('http://192.168.0.15:8080/requests/status.xml');
$sxe = new SimpleXMLElement($file);

$artist_xpath = $sxe->xpath('//info[@name="artist"]');
$album_xpath = $sxe->xpath('//info[@name="album"]');
$title_xpath = $sxe->xpath('//info[@name="title"]');


$artist = (string) $artist_xpath[0];
$album = (string) $album_xpath[0];
$title = (string) $title_xpath[0];


echo "<B>Artist: </B>".$artist."</br>";
echo "<B>Title: </B>".$title."</br>";
echo "<B>Album: </B>".$album."</br>";
?>

您可以使用XPath獲得相同的結果,而不是使用for循環:

// Extraction splitted across two lines for clarity
$artist_xpath = $sxe->xpath('//info[@name="artist"]');
$artist = (string) $artist_xpath[0];
echo $artist;

你必須調整xpath表達式(即適當地改變@name=... ),但你明白了。 另請注意, [0]是必需的,因為xpath將返回匹配數組(並且您只需要第一個),而cast (string)用於提取節點中包含的文本。

此外,您的XML無效,並且由於<info name="album">標記中的文字&出現而被解析器拒絕。

如果再次查看代碼,則缺少一個函數,它將xpath表達式的第一個結果轉換為SimpleXMLElement(強制轉換)的字符串。

寫一次的一種方法是從SimpleXMLElement 擴展

class BetterXMLElement extends SimpleXMLElement
{
    public function xpathString($expression) {
        list($result) = $this->xpath($expression);
        return (string) $result;
    }
}

然后,您創建了更具體的SimpleXMLElement,就像之前使用的那樣:

$file = file_get_contents('http://192.168.0.15:8080/requests/status.xml');
$sxe  = new BetterXMLElement($file);

然后您將從以下代碼中受益:

$artist = $sxe->xpathString('//info[@name="artist"]');
$album  = $sxe->xpathString('//info[@name="album"]');
$title  = $sxe->xpathString('//info[@name="title"]');

echo "<B>Artist: </B>".$artist."</br>";
echo "<B>Title: </B>".$title."</br>";
echo "<B>Album: </B>".$album."</br>";

這讓您重復一些代碼。 這意味着更少的地方你可以犯錯:)

當然,您可以通過允許傳遞多個xpath查詢的數組並返回所有名稱的值來進一步優化它。 但是,根據您的具體需求,您需要自己編寫。 所以使用你在編程中學到的東西來使編程更容易:)

如果你想要更多的建議,下面是使用DOMDocument的另一個非常詳細的例子, DOMDocumentSimpleXML的姐妹庫。 它非常先進但可能會給你一些很好的靈感,我認為使用SimpleXML也可能有類似的東西,這可能是你最終要尋找的東西:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM