Twitter API的致命错误无法使用stdClass类型的对象作为数组

时间:2014-04-16 15:32:41

标签: php api twitter

当我尝试使用小部件获取最新的五条推文时,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>';
    }

1 个答案:

答案 0 :(得分:0)

如果您想将$tweets用作array而不是object,请执行此操作。

$tweets = json_decode(json_encode($tweets), true);

或者您可以尝试使用(array)

进行投射