【发布时间】:2016-08-17 00:38:36
【问题描述】:
我将不胜感激。我正在尝试使用“file_get_contents”从 xml 提要中保存图像。但问题是,它现在只需要一张图片,而且在输入 URL 时也是如此。我正在寻找一个代码,它会在每次刷新提要时自动保存提要中的图像,但只保存新图像,而不是已经存储的图像。另外,问题是,该链接位于“ContentItem”标签内,不容易提取
这是我的 XML 提要代码:
<xml>
<channel><title></title>
<link>http://www.yournewssite.com</link>
<description>gossip</description>
<item>
<title>title of article</title>
<link>http://yourwebsite.com/</link>
<description><![CDATA[<img src=http://yourwebsite.com/i.php?k=d88d4e2b336966b5389837832 width=100 height=100>
<BR>article content<BR>]]></description>
<ContentItem Href="http://yourwebiste.com/i.php?k=d88d4e2b336966b538983783230051c7">
<MediaType FormalName="Picture" />
<MimeType FormalName="image/jpg" />
<Property FormalName="caption" value="Nick Carter" />
</ContentItem>
</xml>
PHP 代码:
<?php
$doc = new DOMDocument();
$doc->load('http://yourwebsite.com/clients/d51b83e5/index.xml');
$xpath = new DOMXpath($doc);
$nodeLists = $xpath->query ('//ContentItem[@Href]');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'ContentItem'=>$nodeLists->item(0)->getAttribute('Href'),
);
array_push($arrFeeds, $itemRSS);
}
echo file_get_contents("http://yourwebsite.com/clients/d51b83e5/index.xml");
$url = '';
$img = 'C:\xampp\htdocs\trial\images\image.jpg';
file_put_contents($img, file_get_contents($url));
?>
非常感谢您对此提供的帮助。提前致谢。
【问题讨论】: