Welcome Guest, Not a member yet? Register   Sign In
Twitter Library
#1

[eluser]Iwasignited[/eluser]
Hello,
Im trying to get some tweets on twitter and display them.
I use Elliot Haughin's library to get the tweets but I cant show them up.

Code:
$tweets = $this->twitter->search('search', array('q' => 'something'));
foreach ($tweets as $tweet) {
echo $tweet->text;
}

It doesn't work.

Please give me some advices

Thanks.
#2

[eluser]Ben Edmunds[/eluser]
I've never used Elliot's twitter library but you can start debugging by doing a print_r($tweets) to see what, if anything, the library call is returning.
#3

[eluser]Iwasignited[/eluser]
Thanks for your reply. Here is the result
Quote:Array
(
[0] => stdClass Object
(
[profile_image_url] => http://s.twimg.com/a/1269020849/images/d...normal.png
[created_at] => Sat, 27 Mar 2010 17:09:23 +0000
[from_user] => articles12
[to_user_id] =>
[text] => What Is A Fountain Pen? http://cli.gs/VAP9P #Business
[id] => 11152972055
[from_user_id] => 100332938
[geo] =>
[iso_language_code] => en
[source] => <a href="http://www.joedolson.com/articles/wp-to-twitter/" rel="nofollow">WP to Twitter</a>
)

[1] => stdClass Object
(
[profile_image_url] => http://s.twimg.com/a/1268953999/images/d...normal.png
[created_at] => Sat, 27 Mar 2010 17:09:09 +0000
[from_user] => onlineworld12
[to_user_id] =>
[text] => Some Things That You Need To Know About Seattle Locksmiths http://cli.gs/ZtX8e #Business
[id] => 11152962722
[from_user_id] => 101167396
[geo] =>
[iso_language_code] => en
[source] => <a href="http://www.joedolson.com/articles/wp-to-twitter/" rel="nofollow">WP to Twitter</a>
)
...
#4

[eluser]Ben Edmunds[/eluser]
Well that looks good.

First I want to make sure your foreach loop is in the controller right?

Next step is to remove the print_r you added and add a new print_r($tweet) inside the foreach; also echo the key, so change your code to:

Code:
$tweets = $this->twitter->search('search', array('q' => 'something'));
foreach ($tweets as $key => $tweet) {
   echo '<br /> Key: ' . $key . '<br />';
   echo '<pre>';
   print_r($tweet);
   echo '</pre><br />';
   echo $tweet->text;
}
#5

[eluser]Iwasignited[/eluser]
The print_r still works.
The keys I got:
Key: results

Key: max_id

Key: since_id

Key: refresh_url

Key: next_page

Key: results_per_page

Key: page

Key: completed_in

Key: warning

Key: query

tweet->text cannot show, I got 'Trying to get property of non-object' errors.
#6

[eluser]Ben Edmunds[/eluser]
Please list the print_r()s for each object inside the foreach.
#7

[eluser]Iwasignited[/eluser]
Quote:Key: results

Array
(
[0] => stdClass Object
(
[profile_image_url] => http://a3.twimg.com/profile_images/77182...normal.jpg
[created_at] => Sun, 28 Mar 2010 06:23:09 +0000
[from_user] => BLK_Nappiness
[to_user_id] =>
[text] => It is such a simple thing, but something that I need to keep reminding myself of.
[id] => 11182989108
[from_user_id] => 104651375
[geo] =>
[iso_language_code] => en
[source] => <a href="http://twitter.com/">web</a>
)

[1] => stdClass Object
(
[profile_image_url] => http://a1.twimg.com/profile_images/68560...normal.jpg
[created_at] => Sun, 28 Mar 2010 06:23:09 +0000
[from_user] => kt7
[to_user_id] => 700975
[text] => @ElizabethThe LOL. Yup. I kinda want a "quadrupole threat". Sings, plays guitar,piano, maybe something else. hahaha. But we'll see. Big Grin
[id] => 11182989011
[from_user_id] => 4099690
[to_user] => elizabeththe
[geo] =>
[iso_language_code] => en
[source] => <a href="http://twitter.com/">web</a>
)
...
#8

[eluser]Iwasignited[/eluser]
and other keys are being showed up:

Quote:Key: max_id

11182989108

Key: since_id

0
...

$tweet->text cannot be showed, receive a PHP error instead:
Quote:Severity: Notice

Message: Trying to get property of non-object
#9

[eluser]Ben Edmunds[/eluser]
Thats weird dude.

So, it looks like you need to do something like

Code:
foreach ($tweets->result as $tweet) {
   echo $tweet->text;
}

Let me know what you get. And are you sure the first time you did print_r($tweets) and not print_r($tweet) or something like that?
#10

[eluser]Iwasignited[/eluser]
It is working now. I got exactly what I need.
Thank you very much.

Maybe I made a mistake in the first time.




Theme © iAndrew 2016 - Forum software by © MyBB