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

如何在php中解析rssfeed中item标记下的xml itunes,php,json,xml,itunes,Php,Json,Xml,Itunes,我想在php中将itunes:image::href=“#”从xml rss提要解析为json。我可以获得标题、发布日期等,但无法解析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/

我想在php中将itunes:image::href=“#”从xml rss提要解析为json。我可以获得标题、发布日期等,但无法解析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>

萨芬娜协会播客
http://www.safinasociety.org
2018年3月5日星期一02:43:02+0000
2018年3月5日星期一02:43:02+0000
60
EN
版权所有
feeds@soundcloud.com(SoundCloud提要)
此生只是一次短暂的考验,此生的最终成功是以一颗善良的心完成安拉的命令,并避免了他的主要禁令。在Safina Society,我们寻求为信徒提供知识和友谊,帮助他们穿越生命的考验,安全地到达生命的终点。
我们希望,这些播客、课程和聚会将引发一场运动,带来新的意图、新的学习、对我们神圣律法的新承诺,以及对我们主的新爱。
这一生只是一次短暂的考验,而最后的考验…
萨芬娜协会
梅因。saiyed@safinasociety.org
萨芬娜协会
不
http://i1.sndcdn.com/avatars-000285408508-ih0xcy-original.jpg
萨芬娜协会
http://www.safinasociety.org
标签:soundcloud,2010:tracks/408792456
S2:E13-给你的太阳植物浇水
2018年3月5日星期一14:23:30+0000
https://soundcloud.com/safina-society/03-01-18a
01:44:20
萨芬娜协会
不
在本期节目中,当我们谈论穆斯林西班牙的崛起、衰落以及我们应该如何理解文明时,团队访问了一些历史。我们还讨论了时代的终结,以及在时代终结时我们应该如何处理自己、乌玛和伊曼人。
在这一集中,团队访问了一些…
在本期节目中,当我们谈论穆斯林西班牙的崛起、衰落以及我们应该如何理解文明时,团队访问了一些历史。我们还讨论了时代的终结,以及在时代终结时我们应该如何处理自己、乌玛和伊曼人。

我想获取itunes:image href=“…我无法使用

获取数据,因为您没有声明用于处理RSS提要的方法。我假设它是
DOMDocument
DOMXPath
——所以诀窍是通过注册该/那些名称空间来使用RSS属性中声明的名称空间

您当然可以使用url而不是本地文件,但由于我不知道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');

    }

?>

通过rss本身收集的url:

$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 />';

}

修改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 );
        }
    }
}
define('BR','
');/*为了方便起见*/ $file='1http://feeds.soundcloud.com/users/soundcloud:users:61234965/sounds.rss'; $dom=新的DOMDocument; $dom->load($file); $xp=新的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 |//item/itunes:author |//item/guid |//item/title |//item/itunes:duration |//item/enclosure'; $col=$xp->query($query); 如果($col->length>0){ foreach($col作为$node){ $tag=$node->tagName; $attr=$node->attributes; $value=$node->nodeValue; 如果($attr->length>0){ 如果($tag=='itunes:image')echo$node->getAttribute('href')。BR; 如果($tag=='enclosure')printf('%s',$node->getAttribute('url'),$node->getAttribute('url'),BR); }否则{ 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
tag:title->value:Democracy&自由主义的未来:与拉希德·达尔一起
标签:itunes:duration->value:01:43:09
标签:itunes:author->value:Safina Society

http://i1.sndcdn.com/artworks-000315497973-5ksx4r-original.jpg
标签:title->value:S2:E13-给你的太阳植物浇水
标签:itunes:duration->value:01:44:20
标签:itunes:author->value:Safina Society

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

我相信您可以考虑如何在上面给出的查询中添加其他元素

请再添加一点细节-rss提要和您到目前为止所拥有的内容我已经更新了我的问题,请检查..提前感谢xml/rss片段省略了模式/名称空间声明,您还没有包含到目前为止使用的任何php或javascript代码o处理此问题。您是否使用PHP DOMDocument和XPath、Javascript或其他库?谢谢RamRaider…我已更新了rss源中的代码…请检查…谢谢。谢谢RamRaider。它正在工作..!!但是如何使用此项目图像获取其他数据?
“如何使用此项目图像获取其他数据?”“
-请解释一下-我不明白你的意思。您是否希望获得其他项目,如
itunes:author
itunes:subtitle
等?是的,我希望获得itunes:author、MP3文件、持续时间、标题、说明、内容、itunes:image和enclosure等..项目0标题:“民主与自由主义的未来:与Rashid Dar”链接:“说明:“民主与自由主义的未来:萨芬娜协会的拉希德·达尔”描述文本:“民主与自由主义的未来:萨芬娜协会的拉希德·达尔”发布日期:“周二,2018年3月13日02:06:45+0000”。采用json格式。
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