2016-06-12 55 views
1

您好,我需要一些幫助,從我的XML rss提要中獲取itunes:image屬性的圖像URL。我可以檢索數組中的其他所有內容,但不能'mainimg'。儘管它在那裏,但當前的代碼沒有返回任何圖像。你有什麼想法我可以做到這一點,我正在尋找的HREF,所以我可以把它放在'img'標籤。RSS XML顯示iTunes:PHP中的圖像url

<?php 
    $doc = new DOMDocument(); 
    $doc->load('http://rss.acast.com/globalpillage'); 
    $cnt=0; 

    foreach ($doc->getElementsByTagName('item') as $node) { 
    $itemRSS = array ( 
    'maintitle' => $node->getElementsByTagName('title')->item(0)->nodeValue, 
    'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 
    'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue, 
    'enclosure' => $node->getElementsByTagName('enclosure')->item(0)->getAttribute('url'), 
    'mainimg' => $node->getElementsByTagName('image')->item(0)->getAttribute('url')/*This is what I'm trying to call the itunes:image attribute(HREF) with*/, 
    ); 

    ?> 
    <div class="showcontent"> 
     <img src="<?php echo $itemRSS['image']; /*echo the itunes:image attribute(HREF)*/ ?>"> 
     <h2><a href="<?php echo $itemRSS['link']; ?>"><?php echo $itemRSS['title']; ?></a></h2> 
     <p><strong>Published</strong> <?php echo $itemRSS['date']; ?></p> 

     <audio controls> 
      <source src="<?php echo $itemRSS['enclosure']; ?>" type="audio/mpeg"> 
     Your browser does not support the audio element. 
     </audio> 
    </div> 

    <?php $cnt ++; } ?> 
+0

'回聲$ HREF = $ dom-> getElementsByTagNameNS( 'http://www.itunes.com/dtds/podcast-1.0.dtd', '圖像') - >項(0) - > getAttribute('href');' – splash58

+0

釘住它!乾杯兄弟。我如何給你學分? –

+0

我做出了答案。你可以接受它 – splash58

回答

0
// Get namespace URI for prefix `itunes` 
echo $ns = $s->lookupNamespaceURI('itunes'); 
// Get `image` tag from that namespace 
echo $href = $node->getElementsByTagNameNS($ns, 'image')->item(0)->getAttribute('href'); 
+0

謝謝你的採訪。當我將它添加到我的數組時,它會引發500錯誤。你如何從itunes中調用圖像:圖像名稱空間? –