开发者

problem in 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 "<p开发者_StackOverflowre>";
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


Try taking that @ symbol from the $xml variable name in the foreach. You only need it to suppress warnings on the call to simplexml_load_file().


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

if(count($xml->posts))
{
  foreach($xml->posts as $post)
  {
    //...
  }
}


foreach($xml->posts->post as $post)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜