当我尝试使用小部件获取最新的五条推文时,PHP会返回此错误:
[Wed Apr 16 12:22:19.839947 2014] [:error] [pid 1987] [client 127.0.0.1:XXXX] PHP Fatal error: Cannot use object of type stdClass as array in /home/user/websites/html/dev_twitterwidget/index.php on line 31
和第31行有这段代码:
<a href="http://twitter.com/'.$tweet['from_user'].'" target="_blank">
获取推文的所有代码:
# Create the connection
$twitter = new TwitterOAuth(CONSUMERKEY, CONSUMERSECRET, ACCESSTOKEN, ACCESSTOKENSECRET);
# Migrate over to SSL/TLS
$twitter->ssl_verifypeer = true;
# Load the Tweets
$tweets = $twitter->get('statuses/user_timeline', array('screen_name' => TWITTERUSERNAME, 'exclude_replies' => 'true', 'include_rts' => 'false', 'count' => TWEETLIMIT));
# Put this after fetching Tweets
$twitter = '';
# Create the HTML output
if(!empty($tweets)) {
foreach($tweets as $tweet) {
$twitter .= '<article>
<aside class="avatar">
<a href="http://twitter.com/'.$tweet['from_user'].'" target="_blank">
<img alt="'.$tweet['from_user'].'" src="'.$tweet['user']['profile_image_url'].'" />
</a>
</aside>
<p>'.$tweet['created_at'].'</p>
<p>'.$tweet['text'].'</p>
</article>';
}
答案 0 :(得分:0)
如果您想将$tweets
用作array
而不是object
,请执行此操作。
$tweets = json_decode(json_encode($tweets), true);
或者您可以尝试使用(array)