我有一个使用XML路径的旧站点,读取它并显示所需的内容。
我现在用它来创建一个新页面,使用不同的XML-URL。
<?xml version="1.0" encoding="UTF-8" ?>
<items>
<item>
<link>http://www.url.to/image.html</link>
<itemname>Teapot</itemname>
<city>Munich</city>
<countryid>DE</countryid>
<desc>
<![CDATA[Here comes the Item Description]]>
</desc>
<imageurl>http://www.url.to/image.jpg</imageurl>
</item>
</items>
条目就是这样被创建的。简单明了。
生成站点的PHP代码现在看起来像这样简单:<?php
//###################Config Start#########################
$maxAnzahl = 50;
$zaehler = 4;
//###################Config End#########################
$feed = simplexml_load_file('http://www.domain.tld/path-to-xml.php&filter1=rule1&filter2=rule2');
$items = $feed->items;
foreach ($items->item as $item) {
if ($item->city == "Munich") {
echo "<div class='4u'>";
echo "<article class='box style2'>";
echo "<a href='".link."' class='image featured' target='_blank'><img src='".imageurl."' alt=".itemname." /> </a>";
echo "<h3><a href='".link."'>".itemname."</a></h3>";
echo "</article>";
echo "</div>";
}
$zaehler = $zaehler + 1;
if ($zaehler == $maxAnzahl) {
break;
}
}
?>
据我所见,应该没问题。但是,当我尝试加载页面时,它告诉我警告:为foreach()在/home/user/public_html/testfolder/index.php中提供的无效参数在第31行而第31行将是foreach ($items->item as $item) {
看起来我在某个地方打破了原始脚本,但我已经使用Winmerge来比较两个文件,所有的差异是我从配置部分踢出了一些变量,因为它们在这里不再使用,不同的"标签"需要在xml url中寻找。
有人能从中做出点什么吗?
请Caylean
必须使用
$feed = simplexml_load_file('http://www.domain.tld/path-to-xml.php&filter1=rule1&filter2=rule2');
foreach($feed->item as $item){
根元素items
在$feed
中不存在。另外,你必须检查$feed->item
是否为array
。
的例子:
<?xml version="1.0" encoding="UTF-8" ?>
<items>
<item>
<link>http://www.url.to/image.html</link>
<itemname>Teapot</itemname>
<city>Munich</city>
<countryid>DE</countryid>
<desc>
<![CDATA[Here comes the Item Description]]>
</desc>
<imageurl>http://www.url.to/image.jpg</imageurl>
</item>
<item>
<link>http://www.url.to/image.html</link>
<itemname>Teapot</itemname>
<city>Munich</city>
<countryid>DE</countryid>
<desc>
<![CDATA[Here comes the Item Description]]>
</desc>
<imageurl>http://www.url.to/image.jpg</imageurl>
</item>
</items>
现在$feed->item
是SimpleXMLElement
。
在这个例子中:
<?xml version="1.0" encoding="UTF-8" ?>
<items>
<item>
<link>http://www.url.to/image.html</link>
<itemname>Teapot</itemname>
<city>Munich</city>
<countryid>DE</countryid>
<desc>
<![CDATA[Here comes the Item Description]]>
</desc>
<imageurl>http://www.url.to/image.jpg</imageurl>
</item>
</items>
$feed->item
将是array
或2 SimpleXMLElement
s。