foreach() 中的问题
<?php
$name=$_REQUEST['tumblr_id'];
if($name=="")
{
$name=$getPageTumblrName;
}
$pCount=1;
$photoPosts=$img_post;
$numPosts = 7;
$name=$getPageTumblrName;
echo $name;
$feedURL = "http://$name.tumblr.com/api/read/?num=$numPosts";
$xml = @simplexml_load_file($feedURL);
echo "<pre>";
print_r($xml);
echo "</pre>";
foreach(@$xml->posts->post as $post)
{
switch ($post['type']) {
case 'photo':
$photo[] = (string) $post->{'photo-caption'};
$img[] = (string) $post->{'photo-url'};
if($pCount==$photoPosts)
// echo "Here are your recent photo posts";
for($i=0;$i<$photoPosts;$i++)
{
if(isset($img[$i]))
{
echo "<div style='width:518px;height:350px;border-bottom: 1px solid;margin:0px auto;'><div style='width:210px;height:200px;float:left;'>".'<img style="width:200px;height:200px;" src="' . $img[$i] . '" />'."</div><div style='width:300px;height:150px;float:right;'>".@substr($photo[$i],0,320)."</div></div><br>";
}
}
$pCount=$pCount+1;
// else
// {
// echo "You have no recent uploaded photo posts";
// }
break;
case 'regular':
$title= (string) $post->{'regular-title'};
$body= (string) $post->{'regular-body'};
$small_post = substr($body,0,320);
echo "<div style='width:518px;height:250px;border-bottom: 1px solid;'><div style='width:518px;height:50px;float:left;'>". $title ."</div><div style='width:518px;height:200px;float:left;'>".$small_post."</div></div><br>";
break;
case 'audio':
$audio= (string) $post->{'audio-caption'};
$audioply= (string) $post->{'audio-player'};
$idtitle= (string) $post->{'id3-title'};
$idartist= (string) $post->{'id3-artist'};
echo "<div style='width:518px;height:100px;border-bottom: 1px solid;'><div style='width:518px;height:50px;float:left;'>". $audio ."</div><div style='width:518px;height:50px;float:left;'>".$audioply."</div></div><br>";
break;
case 'link':
$link= (string) $post->{'link-text'};
$linkul= (string) $post->{'link-url'};
$linkdes= (string) $post->{'link-description'};
echo "<div style='width:518px;height:350px;border-bottom: 1px solid;'><div style='width:518px;height:50px;float:left;'>". $link ."</div><div style='width:518px;height:50px;float:left;'>"."<a href='".$linkul."'>"."treesandboots"."</a></div><div style='width:518px;height:200px;float:left;'>".@substr($linkdes,0,320)."</div></div><br>";
break;
case 'quote':
$text= (string) $post->{'quote-text'};
$quote= (string) $post->{'quote-source'};
echo "<div style='width:518px;height:150px;border-bottom: 1px solid;'><div style='width:518px;height:50px;float:left;'>" . $text ."</div><div style='width:518px;height:100px;float:left;'>".$quote."</div></div><br>";
break;
case 'video':
$video= (string) $post->{'video-caption'};
$videocap= (string) $post->{'video-source'};
$videoply= (string) $post->{'video-player'};
echo "<div style='width:518px;height:400px;border-bottom: 1px solid;'><div style='width:518px;height:50px;float:left;'>". $video ."</div><div style='width:518px;height:350px;float:left;'>".$videoply."</div></div><br>";
break;
default:
echo "no post available";
break;
}
}
?>
我正在使用这个脚本但收到错误:-
Warning: Invalid argument supplied for foreach() in /public_html/samples/tab/tumblr/controllers/feed.php on line 19
<?php
$name=$_REQUEST['tumblr_id'];
if($name=="")
{
$name=$getPageTumblrName;
}
$pCount=1;
$photoPosts=$img_post;
$numPosts = 7;
$name=$getPageTumblrName;
echo $name;
$feedURL = "http://$name.tumblr.com/api/read/?num=$numPosts";
$xml = @simplexml_load_file($feedURL);
echo "<pre>";
print_r($xml);
echo "</pre>";
foreach(@$xml->posts->post as $post)
{
switch ($post['type']) {
case 'photo':
$photo[] = (string) $post->{'photo-caption'};
$img[] = (string) $post->{'photo-url'};
if($pCount==$photoPosts)
// echo "Here are your recent photo posts";
for($i=0;$i<$photoPosts;$i++)
{
if(isset($img[$i]))
{
echo "<div style='width:518px;height:350px;border-bottom: 1px solid;margin:0px auto;'><div style='width:210px;height:200px;float:left;'>".'<img style="width:200px;height:200px;" src="' . $img[$i] . '" />'."</div><div style='width:300px;height:150px;float:right;'>".@substr($photo[$i],0,320)."</div></div><br>";
}
}
$pCount=$pCount+1;
// else
// {
// echo "You have no recent uploaded photo posts";
// }
break;
case 'regular':
$title= (string) $post->{'regular-title'};
$body= (string) $post->{'regular-body'};
$small_post = substr($body,0,320);
echo "<div style='width:518px;height:250px;border-bottom: 1px solid;'><div style='width:518px;height:50px;float:left;'>". $title ."</div><div style='width:518px;height:200px;float:left;'>".$small_post."</div></div><br>";
break;
case 'audio':
$audio= (string) $post->{'audio-caption'};
$audioply= (string) $post->{'audio-player'};
$idtitle= (string) $post->{'id3-title'};
$idartist= (string) $post->{'id3-artist'};
echo "<div style='width:518px;height:100px;border-bottom: 1px solid;'><div style='width:518px;height:50px;float:left;'>". $audio ."</div><div style='width:518px;height:50px;float:left;'>".$audioply."</div></div><br>";
break;
case 'link':
$link= (string) $post->{'link-text'};
$linkul= (string) $post->{'link-url'};
$linkdes= (string) $post->{'link-description'};
echo "<div style='width:518px;height:350px;border-bottom: 1px solid;'><div style='width:518px;height:50px;float:left;'>". $link ."</div><div style='width:518px;height:50px;float:left;'>"."<a href='".$linkul."'>"."treesandboots"."</a></div><div style='width:518px;height:200px;float:left;'>".@substr($linkdes,0,320)."</div></div><br>";
break;
case 'quote':
$text= (string) $post->{'quote-text'};
$quote= (string) $post->{'quote-source'};
echo "<div style='width:518px;height:150px;border-bottom: 1px solid;'><div style='width:518px;height:50px;float:left;'>" . $text ."</div><div style='width:518px;height:100px;float:left;'>".$quote."</div></div><br>";
break;
case 'video':
$video= (string) $post->{'video-caption'};
$videocap= (string) $post->{'video-source'};
$videoply= (string) $post->{'video-player'};
echo "<div style='width:518px;height:400px;border-bottom: 1px solid;'><div style='width:518px;height:50px;float:left;'>". $video ."</div><div style='width:518px;height:350px;float:left;'>".$videoply."</div></div><br>";
break;
default:
echo "no post available";
break;
}
}
?>
I am using this script but getting the error :-
Warning: Invalid argument supplied for foreach() in /public_html/samples/tab/tumblr/controllers/feed.php on line 19
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来 foreach 中的第一个参数不是数组。
根据大多数 RSS 提要,我假设您需要
foreach(@$xml->posts as $post)
。我也更喜欢将 foreach 包装在 if 语句中
It looks like the first argument in the for each is not an array.
Going by most RSS feeds, I assume you want
foreach(@$xml->posts as $post)
.I also prefer to wrap my foreach's in an if statement
尝试从
foreach
中的$xml
变量名称中获取@
符号。您只需要它来抑制调用simplexml_load_file()
时出现的警告。Try taking that
@
symbol from the$xml
variable name in theforeach
. You only need it to suppress warnings on the call tosimplexml_load_file()
.