0

我检索了一个 json 格式的 twitter 提要,当我在我的 apacheserver 上运行这个块时,它工作正常,并提供了一个带有 twitter 提要的漂亮表格。

但是,当放置在 symfony2 控制器中时,它会因以下错误消息而中断:

Notice: Undefined property: stdClass::$retweeted_status in /var/www/cloudsign_beta/src/BizTV/ArchiveBundle/Controller/DefaultController.php line 76

编码:

public function TwitterAction($rts, $name, $count)
{

    if ($rts=='on') {
        $rts = 'true';
    }
    else {
        $rts = 'false';
    }

    if ($name=='') {
        //$name = 'warpnine'; 
        //Bazinga!
    }

    if ($count=='') {
        $count = 10;
    }

    $variable = file_get_contents('https://api.twitter.com/1/statuses/user_timeline.json?include_rts='.$rts.'&screen_name='.$name.'&count='.$count);
    $tws = json_decode($variable);

    if (isset($tws)) {
        $r = '<table>';
            foreach ($tws as $tw){
                if ($tw->retweeted_status->id > 0) {
                    $r .='<tr><td class="twitter_td"><img src="'.$tw->retweeted_status->user->profile_image_url.'" class="twitter_img"></td><td>'.
                                $tw->retweeted_status->user->name.
                                ' <i>@'.$tw->retweeted_status->user->screen_name.'</i><br>'.
                                $tw->text.'</td></tr>';
                } else {
                    $r .= '<tr><td class="twitter_td"><img src="'.$tw->user->profile_image_url.'" class="twitter_img"></td><td>'.
                                $tw->user->name.
                                ' <i>@'.$tw->user->screen_name.'</i><br>'.
                                $tw->text.'</td></tr>';
                }
            }
        $r .= '</table>';
    }
    else {
        $r = "Hittade ingen twitterfeed för <strong>$name</strong>";
    }

    return new Response($r);

}   

这是 $tw 第一次迭代的 var_dump

object(stdClass)#878 (19) { ["created_at"]=> string(30) "Tue Jan 15 15:48:55 +0000 2013" ["id"]=> int(291210363318435841) ["id_str"]=> string(18) "291210363318435841" ["text"]=> string(97) "Looking forward to speaking at Washington and Lee University in Lexington, Virginia, later today!" ["source"]=> string(63) "TweetDeck" ["truncated"]=> bool(false) ["in_reply_to_status_id"]=> NULL ["in_reply_to_status_id_str"]=> NULL ["in_reply_to_user_id"]=> NULL ["in_reply_to_user_id_str"]=> NULL ["in_reply_to_screen_name"]=> NULL ["user"]=> object(stdClass)#875 (38) { ["id"]=> int(287413569) ["id_str"]=> string(9) "287413569" ["name"]=> string(8) "Ron Paul" ["screen_name"]=> string(7) "RonPaul" ["location"]=> string(27) "Clute, TX / Washington D.C." ["url"]=> NULL ["description"]=> string(99) "Former Congressman from Texas, Chairman of Campaign for Liberty, and Champion of the Constitution. " ["protected"]=> bool(false) ["followers_count"]=> int(387763) ["friends_count"]=> int(159) ["listed_count"]=> int(4852) ["created_at"]=> string(30) "Sun Apr 24 23:20:55 +0000 2011" ["favourites_count"]=> int(1) ["utc_offset"]=> int(-18000) ["time_zone"]=> string(26) "Eastern Time (US & Canada)" ["geo_enabled"]=> bool(false) ["verified"]=> bool(true) ["statuses_count"]=> int(976) ["lang"]=> string(2) "en" ["contributors_enabled"]=> bool(false) ["is_translator"]=> bool(false) ["profile_background_color"]=> string(6) "ED1D24" ["profile_background_image_url"]=> string(93) "http://a0.twimg.com/profile_background_images/758570925/9c556d05e88c52861e85356d0cbf1a4c.jpeg" ["profile_background_image_url_https"]=> string(95) "https://si0.twimg.com/profile_background_images/758570925/9c556d05e88c52861e85356d0cbf1a4c.jpeg" ["profile_background_tile"]=> bool(false) ["profile_image_url"]=> string(90) "http://a0.twimg.com/profile_images/3066929869/81d574c68b91e40db69992c0afd34d29_normal.jpeg" ["profile_image_url_https"]=> string(92) "https://si0.twimg.com/profile_images/3066929869/81d574c68b91e40db69992c0afd34d29_normal.jpeg" ["profile_banner_url"]=> string(58) "https://si0.twimg.com/profile_banners/287413569/1357674836" ["profile_link_color"]=> string(6) "DE0A22" ["profile_sidebar_border_color"]=> string(6) "FFFFFF" ["profile_sidebar_fill_color"]=> string(6) "FFFFFF" ["profile_text_color"]=> string(6) "333333" ["profile_use_background_image"]=> bool(true) ["default_profile"]=> bool(false) ["default_profile_image"]=> bool(false) ["following"]=> NULL ["follow_request_sent"]=> NULL ["notifications"]=> NULL } ["geo"]=> NULL ["coordinates"]=> NULL ["place"]=> NULL ["contributors"]=> NULL ["retweet_count"]=> int(96) ["favorited"]=> bool(false) ["retweeted"]=> bool(false) }
4

1 回答 1

0

注意:未定义的属性意味着在这种情况下不存在“转推状态”(对于循环中的此项)。正如评论中提到的,sf2 默认情况下具有严格模式(改进代码的好东西)。

尝试添加类似

if(isset($tw->retweeted_status) { }

所以你的代码看起来像

foreach ($tws as $tw){
    if(isset($tw->retweeted_status) {
            if ($tw->retweeted_status->id > 0) {
                $r .='<tr><td class="twitter_td"><img src="'.$tw->retweeted_status->user->profile_image_url.'" class="twitter_img"></td><td>'.
                            $tw->retweeted_status->user->name.
                            ' <i>@'.$tw->retweeted_status->user->screen_name.'</i><br>'.
                            $tw->text.'</td></tr>';
            } else {
                $r .= '<tr><td class="twitter_td"><img src="'.$tw->user->profile_image_url.'" class="twitter_img"></td><td>'.
                            $tw->user->name.
                            ' <i>@'.$tw->user->screen_name.'</i><br>'.
                            $tw->text.'</td></tr>';
            }
    }
}
于 2013-01-16T10:13:11.963 回答