代码适用于 PHP 5.3.2。在 PHP 5.2.17 中,为 foreach() 提供的参数无效
我正在使用这段代码:-
<?php // Load and parse the XML document
$rss = simplexml_load_file('http://partners.userland.com/nytRss/nytHomepage.xml');
$title = $rss->channel->title;
?>
<html xml:lang="en" lang="en">
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title; ?></h1>
<?php
// Here we'll put a loop to include each item's title and description
foreach ($rss->channel->item as $item) {
echo "<h2><a href='" . $item->link . "'>" . $item->title . "</a></h2>";
echo "<p>" . $item->description . "</p>";
}
?>
</body>
</html>
这是我从 www.ibm.com/developerworks/library/x-simplexml.html 站点获得的代码
我有一个令人费解的问题。
当我在开发服务器上运行代码时,它可以正常工作。
当我在我的网络主机服务器上运行它时,我收到此错误报告:-
警告:第 15 行 /web1/......................../test3.php 中的 foreach() 提供的参数无效
我的开发服务器是带有 PHP 5.3.2 的 TurnKey Linux LAMP 服务器。
我的网络主机在 Linux 上运行 PHP 5.2.17。
在网上查找错误消息似乎表明 PHP 5.2.17 没有将从 XML feed 读取的数据视为数组。
我尝试过的“无效参数....foreach()”下的解决方案无法解决该问题。
关于如何解决这个问题有什么想法吗?
I am using this code:-
<?php // Load and parse the XML document
$rss = simplexml_load_file('http://partners.userland.com/nytRss/nytHomepage.xml');
$title = $rss->channel->title;
?>
<html xml:lang="en" lang="en">
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title; ?></h1>
<?php
// Here we'll put a loop to include each item's title and description
foreach ($rss->channel->item as $item) {
echo "<h2><a href='" . $item->link . "'>" . $item->title . "</a></h2>";
echo "<p>" . $item->description . "</p>";
}
?>
</body>
</html>
Which I got from this site www.ibm.com/developerworks/library/x-simplexml.html
I have one puzzling issue.
When I run the code on my development server it works with no problem.
When I run it on my web host server I get this error report:-
Warning: Invalid argument supplied for foreach() in /web1/............../test3.php on line 15
My development server is a TurnKey Linux LAMP server with PHP 5.3.2.
My web host has PHP 5.2.17 running on Linux.
Looking up the error message on the web seems to indicate that the data read from the XML feed is not being treated as an array by PHP 5.2.17.
The solutions on here under 'Invalid argument....foreach()' that I have tried do not resolve the issue.
Any ideas as to how to get around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来托斯坦的本能已经“按下按钮”了。我的虚拟主机似乎阻止从互联网下载文件。
我使用此代码段尝试将远程文件加载到本地副本:-
if(!@copy('http://partners.userland.com/nytRss/nytHomepage.xml','./buffer. xml'))
生成的文件 buffer.xml 包含以下内容:-
这不是特定于此网址,我在 BBC 新闻源网址上得到相同的信息。
所以,有迹象表明这不是 PHP 的问题!
我已向我的网络主机提供商提出了该问题。
这个问题让我想起的一件事是 PHP 错误消息 &行号可能与实际问题相去甚远!
感谢托斯坦、马里奥和GusDe Cool 帮助我解决了这个问题。
比尔·P
It looks as though Torstein's instincts are 'on the button'; my webhost seems to be blocking downloading files from the internet.
I used this code segment to try to load the distant file to a local copy:-
if(!@copy('http://partners.userland.com/nytRss/nytHomepage.xml','./buffer.xml'))
The resulting file buffer.xml contained this:-
This is not specific to this url, I get the same on a BBC newsfeed url.
So, indications are that it is not a PHP problem!
I have raised the issue with my webhost provider.
One thing this problem has reminded me of is that the PHP error message & line number may be far removed from the actual problem!
Thanks to Torstein, mario & GusDe Cool for helping me get my head around this problem.
BillP
您的虚拟主机似乎已禁止在互联网上打开文件。
我不确定
simplexml_load_file()
是如何工作的,但是如果您在网站上运行phpinfo()
以及allow_url_fopen
和等选项禁用了allow_url_include
,这是一个很好的指示。It looks like your webhost has disabled opening of files on the internet.
I'm not sure how
simplexml_load_file()
works, but if you runphpinfo()
on the website and options likeallow_url_fopen
andallow_url_include
are disabled, thats a good indication.