我需要图像附件的链接。
<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'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>我试过各种置换方法
$this->enclosure->url;
$this->enclosure['url'];等。
即使我尝试获取附件的长度,我仍然可以得到文章的url。
我得到的是“链接”url,而不是附件url。
感谢您的帮助。提前谢谢你。
发布于 2018-06-08 14:49:21
之后,您应该使用getElementsByTagName('enclosure')通过标记名获取DOM元素,然后使用getAttribute('url')从enclosure标记获取url
我的代码
<?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'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');
}https://stackoverflow.com/questions/50754491
复制相似问题