与Twitter API致命错误无法使用类型stdClass的对象为数组 [英] Fatal error with Twitter API Cannot use object of type stdClass as array

查看:181
本文介绍了与Twitter API致命错误无法使用类型stdClass的对象为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图让最近五年的鸣叫与一个小部件,PHP返回此错误:

  [周三12年4月16日:22:19.839947 2014年] [:错误] [1987年的pid] [客户127.0.0.1:XXXX] PHP致命错误:无法使用类型stdClass的对象为数组中/home/user/websites/html/dev_twitterwidget/index.php在线31

和第31行有这样的code:

 < A HREF =htt​​p://twitter.com/'.$tweet['from_user']'目标=_空白>

和所有的code得到鸣叫:

 #创建连接
$叽叽喳喳=新TwitterOAuth对象(CONSUMERKEY,CONSUMERSECRET,的accessToken,ACCESSTOKENSECRET);#迁移到SSL / TLS
$ twitter-> ssl_verifypeer = TRUE;#加载推文
$鸣叫= $ twitter->获取('状态/ user_timeline',阵列('SCREEN_NAME'=> TWITTERUSERNAME,'exclude_replies'=>'真','include_rts'=>假,计数= > TWEETLIMIT));#获取鸣叫后,将这个
$叽叽喳喳='';#创建HTML输出
如果(!空($微博)){
    的foreach($鸣叫为$鸣叫){
        $叽叽喳喳='<文章>
            <一边类=阿凡达>
                < A HREF =htt​​p://twitter.com/'.$tweet['from_user']'目标=_空白>
                    &所述; IMG ALT ='。$鸣叫['FROM_USER']'。SRC ='[用户] ['profile_image_url']'$鸣叫。/>
                &所述; / A>
            < /一旁>
            &所述p为H.;'$鸣叫。['created_at']'&下; / P>
            < P>'$鸣叫。['文本'。'< / P>
        < /条>';
    }


解决方案

如果你想使用 $鸣叫阵列而不是对象做到这一点。

  $鸣叫= json_de code(json_en code($鸣叫),TRUE);

或者你可以尝试用它转换(阵列)

When i try to get the latest five tweets with a widget, PHP returns this error:

[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

and line 31 have this code:

<a href="http://twitter.com/'.$tweet['from_user'].'" target="_blank">

And the all the code to get Tweets:

# 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>';
    }

解决方案

if you want to use $tweets as an array and not an object do this.

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

or you can try to cast it with (array)

这篇关于与Twitter API致命错误无法使用类型stdClass的对象为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆