如何在 php 中的 rssfeed 中的项目标签下解析 xml itunes

How to parse xml itunes under item tag in rssfeed in php

我想将 itunes:image::href="#" 从 xml rss 提要解析为 php 中的 json。我能够获得标题、PubDate 等。但无法解析 itunes..请帮助 这是我的 rssfeed 片段。

 <?xml version='1.0' encoding='UTF-8'?>
<rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:atom="http://www.w3.org/2005/Atom">
      <channel>
        <atom:link href="http://feeds.soundcloud.com/users/soundcloud:users:61234965/sounds.rss" rel="self" type="application/rss+xml"/>
        <atom:link href="http://feeds.soundcloud.com/users/soundcloud:users:61234965/sounds.rss?before=288739617" rel="next" type="application/rss+xml"/>
        <title>The Safina Society Podcast</title>
        <link>http://www.safinasociety.org</link>
        <pubDate>Mon, 05 Mar 2018 02:43:02 +0000</pubDate>
        <lastBuildDate>Mon, 05 Mar 2018 02:43:02 +0000</lastBuildDate>
        <ttl>60</ttl>
        <language>en</language>
        <copyright>All rights reserved</copyright>
        <webMaster>feeds@soundcloud.com (SoundCloud Feeds)</webMaster>
        <description>This life is but a transitory test, and the ultimate success in this life is reaching its end having fulfilled Allah’s commands with a good heart and avoided His major prohibitions. At Safina Society we seek to offer the faithful the knowledge and companionship that will assist in traversing the test of life and reaching its end safely. 

It is our hope that these podcasts, classes and gatherings will spark a movement, of renewed intention, renewed study, renewed commitment to our sacred law, as well as renewed love of our Lord.</description>
        <itunes:subtitle>This life is but a transitory test, and the ultim…</itunes:subtitle>
        <itunes:owner>
          <itunes:name>Safina Society</itunes:name>
          <itunes:email>muin.saiyed@safinasociety.org</itunes:email>
        </itunes:owner>
        <itunes:author>Safina Society</itunes:author>
        <itunes:explicit>no</itunes:explicit>
        <itunes:image href="http://i1.sndcdn.com/avatars-000285408508-ih0xcy-original.jpg"/>
        <image>
          <url>http://i1.sndcdn.com/avatars-000285408508-ih0xcy-original.jpg</url>
          <title>Safina Society</title>
          <link>http://www.safinasociety.org</link>
        </image>
        <itunes:category text="Education"/>
        <item>
      <guid isPermaLink="false">tag:soundcloud,2010:tracks/408792456</guid>
      <title>S2:E13 - Water your Sun Plant</title>
      <pubDate>Mon, 05 Mar 2018 14:23:30 +0000</pubDate>
      <link>https://soundcloud.com/safina-society/03-01-18a</link>
      <itunes:duration>01:44:20</itunes:duration>
      <itunes:author>Safina Society</itunes:author>
      <itunes:explicit>no</itunes:explicit>
      <itunes:summary>Listen as on this episode the team visits some of history as we talk about the rise of Muslim Spain, it's fall and how we should understand civilizations. We also discuss the end of times and how we should handle ourselves, the ummah, and our imaan during the end of times.</itunes:summary>
      <itunes:subtitle>Listen as on this episode the team visits some of…</itunes:subtitle>
      <description>Listen as on this episode the team visits some of history as we talk about the rise of Muslim Spain, it's fall and how we should understand civilizations. We also discuss the end of times and how we should handle ourselves, the ummah, and our imaan during the end of times.</description>
      <enclosure type="audio/mpeg" url="http://feeds.soundcloud.com/stream/408792456-safina-society-03-01-18a.mp3" length="150229913"/>
      <itunes:image href="http://i1.sndcdn.com/artworks-000311333859-zttwpu-original.jpg"/>
    </item>
      </channel>
    </rss>

我想获取 itunes:image href=".....我无法使用

获取数据

由于您没有声明用于处理 RSS 提要的方法,我假设它是 DOMDocumentDOMXPath - 所以诀窍是使用在通过注册那个/那些命名空间的 RSS 属性。

您当然可以使用 url 而不是本地文件,但由于我不知道 url 我无法证明 - 但本质上是一样的。

<?php
    /*
        Assuming that a local ccopy of the RSS exists in the same directory as this script
    */
    $file=__DIR__.'/itunes.rss';
    $dom=new DOMDocument;
    $dom->load( $file );

    $xp=new DOMXPath( $dom );
    $xp->registerNamespace('itunes','http://www.itunes.com/dtds/podcast-1.0.dtd');
    $xp->registerNamespace('atom','http://www.w3.org/2005/Atom');

    $query='//item/itunes:image';
    $col=$xp->query( $query );

    if( $col->length > 0 ){

        foreach( $col as $node ) echo $node->getAttribute('href');

    }

?>

产出

http://i1.sndcdn.com/artworks-000311333859-zttwpu-original.jpg

使用 url,从 rss 本身收集:

$file='http://feeds.soundcloud.com/users/soundcloud:users:61234965/sounds.rss';

$dom=new DOMDocument;
$dom->load( $file );

$xp=new DOMXPath( $dom );
$xp->registerNamespace('itunes','http://www.itunes.com/dtds/podcast-1.0.dtd');
$xp->registerNamespace('atom','http://www.w3.org/2005/Atom');

$query='//item/itunes:image';
$col=$xp->query( $query );

if( $col->length > 0 ){

    foreach( $col as $node ) echo $node->getAttribute('href') . '<br />';

}

输出:

http://i1.sndcdn.com/artworks-000311333859-zttwpu-original.jpg
http://i1.sndcdn.com/artworks-000307423581-cp3yz4-original.jpg
http://i1.sndcdn.com/artworks-000304937310-4lpxwt-original.jpg
http://i1.sndcdn.com/artworks-000303669636-ikttvf-original.jpg
http://i1.sndcdn.com/artworks-000303671391-egwsr9-original.jpg
http://i1.sndcdn.com/artworks-000292343751-dga5d4-original.jpg
http://i1.sndcdn.com/artworks-000287215952-jl1vmx-original.jpg
http://i1.sndcdn.com/artworks-000285476606-3fn1vc-original.jpg
http://i1.sndcdn.com/artworks-000280384667-f5o3rl-original.jpg
http://i1.sndcdn.com/artworks-000277395593-15vyp1-original.jpg
http://i1.sndcdn.com/avatars-000285408508-ih0xcy-origi..... etc etc

将 XPath 查询修改为 select 您感兴趣的任何标签,并根据需要处理它们。例如:

define('BR','<br />');/* for convenience */

$file='http://feeds.soundcloud.com/users/soundcloud:users:61234965/sounds.rss';

$dom=new DOMDocument;
$dom->load( $file );

$xp=new DOMXPath( $dom );
$xp->registerNamespace('itunes','http://www.itunes.com/dtds/podcast-1.0.dtd');
$xp->registerNamespace('atom','http://www.w3.org/2005/Atom');

/* add additional tags to the query */
$query='//item/itunes:image | //item/itunes:author | //item/guid  | //item/title | //item/itunes:duration | //item/enclosure';
$col=$xp->query( $query );

if( $col->length > 0 ){

    foreach( $col as $node ) {

        $tag = $node->tagName;
        $attr = $node->attributes;
        $value = $node->nodeValue;

        if( $attr->length > 0 ){

            if( $tag=='itunes:image' ) echo $node->getAttribute('href') . BR;
            if( $tag=='enclosure' ) printf('<a href="%s" target="_blank">%s</a>%s',$node->getAttribute('url'),$node->getAttribute('url'),BR);

        } else{

            printf('tag: %s ->  value: %s%s',$tag,$value,BR );
        }
    }
}

将输出:

tag: title  ->  value: Democracy & The Future of Liberalism: With Rashid Dar<br />
tag: itunes:duration    ->  value: 01:43:09<br />
tag: itunes:author  ->  value: Safina Society<br />
<a href="http://feeds.soundcloud.com/stream/412842882-safina-society-democracy-the-future-of-liberalism-with-rashid-dar.mp3" target="_blank">http://feeds.soundcloud.com/stream/412842882-safina-society-democracy-the-future-of-liberalism-with-rashid-dar.mp3</a><br />
http://i1.sndcdn.com/artworks-000315497973-5ksx4r-original.jpg<br />

tag: title  ->  value: S2:E13 - Water your Sun Plant<br />
tag: itunes:duration    ->  value: 01:44:20<br />
tag: itunes:author  ->  value: Safina Society<br />
<a href="http://feeds.soundcloud.com/stream/408792456-safina-society-03-01-18a.mp3" target="_blank">http://feeds.soundcloud.com/stream/408792456-safina-society-03-01-18a.mp3</a><br />
http://i1.sndcdn.com/artworks-000311333859-zttwpu-original.jpg

... etc

我相信您可以想出如何将其他元素添加到上述给定的查询中

<?php echo $node->children('itunes', true)->image->attributes()->href;?>