首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析RSS封装URL - PHP

解析RSS封装URL - PHP
EN

Stack Overflow用户
提问于 2018-06-08 14:20:18
回答 1查看 820关注 0票数 0

我需要图像附件的链接。

代码语言:javascript
复制
<item> <title>Patches of Snow on the Red Planet</title>
<link>http://www.nasa.gov/image-feature/patches-of-snow-on-the-red- 
planet</link>
<description>In early Martian summer, at the time NASA&#039;s Mars 
Reconnaissance Orbiter acquired this image, the dunes are almost free of 
their seasonal ice cover.</description>
<enclosure 
url="http://www.nasa.gov/sites/default/files/thumbnails/image/marssnow.jpg" 
length="516834" type="image/jpeg" />
<guid isPermaLink="false">http://www.nasa.gov/image-feature/patches-of-snow- 
on-the-red-planet</guid>
<pubDate>Tue, 05 Jun 2018 15:06 EDT</pubDate>
<source url="http://www.nasa.gov/rss/dyn/image_of_the_day.rss">NASA Image of 
the Day</source>
</item>

我试过各种置换方法

代码语言:javascript
复制
$this->enclosure->url;

$this->enclosure['url'];

等。

即使我尝试获取附件的长度,我仍然可以得到文章的url。

我得到的是“链接”url,而不是附件url。

感谢您的帮助。提前谢谢你。

EN

回答 1

Stack Overflow用户

发布于 2018-06-08 14:49:21

之后,您应该使用getElementsByTagName('enclosure')通过标记名获取DOM元素,然后使用getAttribute('url')enclosure标记获取url

我的代码

代码语言:javascript
复制
<?php
// XML test 
$xml = <<< XML
<item> <title>Patches of Snow on the Red Planet</title>
<link>http://www.nasa.gov/image-feature/patches-of-snow-on-the-red- 
planet</link>
<description>In early Martian summer, at the time NASA&#039;s Mars 
Reconnaissance Orbiter acquired this image, the dunes are almost free of 
their seasonal ice cover.</description>
<enclosure 
url="http://www.nasa.gov/sites/default/files/thumbnails/image/marssnow.jpg" 
length="516834" type="image/jpeg" />
<guid isPermaLink="false">http://www.nasa.gov/image-feature/patches-of-snow- 
on-the-red-planet</guid>
<pubDate>Tue, 05 Jun 2018 15:06 EDT</pubDate>
<source url="http://www.nasa.gov/rss/dyn/image_of_the_day.rss">NASA Image of 
the Day</source>
</item>
XML;

$dom = new DOMDocument;
$dom->loadXML($xml);
$ens = $dom->getElementsByTagName('enclosure');

foreach ($ens as $en)
{
    // you can debug here
    echo $en->getAttribute('url');
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50754491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档