我试图创建一个简单的PHP代码块显示tweets使用twitter API,这只是显示tweets关于EVE Online -作为测试。下面的代码确实可以工作,但是,我在它之后得到以下错误PHP通知:
下面是我的代码:遇到PHP错误
严重性:注意
消息:尝试获取非对象的属性
文件名:页面/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>';
但这也不起作用。请帮助。
明白了,基本上我用这个代替了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>";
}
?>
如果是数组则使用
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>';
编辑
查看本教程