I have retrieved a twitter feed in json format, and when I run this block right on my apacheserver it works fine and delivers a nice table with the twitter feeds.
However, when placed inside a symfony2 controller, it breaks with this error message:
Notice: Undefined property: stdClass::$retweeted_status in /var/www/cloudsign_beta/src/BizTV/ArchiveBundle/Controller/DefaultController.php line 76
The code:
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);
}
Here is a var_dump of the $tw first iteration
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) }
Notice: Undefined property means that ‘retweeted_status’ does not exists in this case (for this item in the loop). As mentioned in the comments, sf2 has strict mode on bydefault (a good thing to improve code).
Try to add something like
so your code would look like