Question

I'm trying to create a simple piece of PHP code to display tweets using the twitter API, that just displays tweets about EVE Online - as a test. The code below does work however, I get the following error PHP notice after it:

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: pages/about.php

Line Number: 17

Here is my code:

<?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>

I'm not using CodeIgniter's controllers or methods to do this. I'm trying to do it outside of CodeIgniter whilst using CodeIgniter for everything else. I'm sure there's a way, however I don't understand why CodeIgniter displays errors when I'm not using any of it's controllers or methods.

I've also tried this:

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

But this also does not work. Please help.

Was it helpful?

Solution

got it, basically i replaced the foreach loop with this:

<?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>";
}
?>

OTHER TIPS

If its an array use this:

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

instead of:

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

EDIT

See this tutorial

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top