消息:尝试获取非对象Codeigniter的属性

时间:2014-03-29 22:16:56

标签: php codeigniter twitter twitter-oauth

我尝试使用twitter API创建一个简单的PHP代码来显示推文,这只是显示关于 EVE Online 的推文 - 作为测试。下面的代码确实有效,我在它之后得到以下错误PHP通知:

  

遇到PHP错误

     

严重性:注意

     

消息:尝试获取非对象的属性

     

文件名:pages / about.php

     

行号:17

这是我的代码:

<?php include('twitteroauth.php');?>
<?php include('get_tweet.php');?>
<div id="tweets">
<?php
    $tweets = $twitter->get('https://api.twitter.com/1.1/search/tweets.jsonq=EveOnline&
    result_type=recent&count=10'    );
foreach($tweets as $tweet)
{
    foreach($tweet as $t)
    {
    echo '<div id="tweetwrap"><img src="'.$t->user->profile_image_url.'"/>'
        .$t->text.'</div>';
    }
}
?>
</div>

我没有使用CodeIgniter的控制器或方法来执行此操作。我尝试在CodeIgniter之外进行此操作,同时将CodeIgniter用于其他所有内容。我确定有一种方法,但是当我不使用任何控制器或方法时,我不明白为什么CodeIgniter会显示错误。

我也试过这个:

    echo '<div id="tweetwrap"><img src="'.$t->user['profile_image_url'].'"/>'
        .$t['text'].'</div>';

但这也行不通。请帮忙。

2 个答案:

答案 0 :(得分:1)

得到它,基本上我用这个取代了 foreach 循环:

<?php include('twitteroauth.php');?>
<?php include('get_tweet.php');?>
<div id="tweets">
<?php
foreach($tweets->statuses as $status)
{
    echo '<div id="tweetwrap"><img src="'.$status->user-
>profile_image_url.'"/>';
    echo "User: " .$status->user->screen_name."</br>";
    echo "<p>Tweet: " .$status->text."</p></br>";
    echo "</br></div>";
}
?>

答案 1 :(得分:0)

如果它的数组使用它:

echo '<div id="tweetwrap"><img src="'.$t['user']['profile_image_url'].'"/>'
    .$t['text'].'</div>';

而不是:

echo '<div id="tweetwrap"><img src="'.$t->user['profile_image_url'].'"/>'
    .$t['text'].'</div>';

修改

See this tutorial