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

时间:2018-03-12 08:03:44

标签: php json xml itunes

我想解析itunes:image :: href =“#”从xml rss feed到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 =“.....我无法使用

获取数据

1 个答案:

答案 0 :(得分:0)

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

你可以使用网址而不是本地文件,但因为我不知道我无法显示的网址 - 但基本上是相同的。

<?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查询以选择您感兴趣的任何标签,然后根据需要处理它们。例如:

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

我确信您可以在上面的

中找出如何将其他元素添加到查询中