Welcome Guest, Not a member yet? Register   Sign In
Library for getting tweets
#1

[eluser]Unknown[/eluser]
Hello.

I work with mobile websites, and costumers have been requesting a page to display their company's latest tweets a lot. I know there are a lot of methods out there for grabbing tweets, but i worked a little in this library, made it easy to use with code igniter and i think it's worth sharing.

Its features:

- Easy to call and configure

- Can retrieve either twitter regular search or user's timeline

- Automatically 'linkifies' links, #hashtags, and @users

- Lets you choose how many tweets you want to retrieve

- Has a recursive caching system, appending new tweets to the cache file, up to how many tweets you configure it to store. This way, if twitter.com is down or overloaded you still have the latest tweets you got.

- Lets you sethow often the cache file should be renewed

- Adds a new entry to each tweet with it's creation date in unix format (useful for sorting chronologically)

- Adds a new entry to each tweets with how much time has passed since its creation in twitter style (10 minutes ago, 3 days ago, etc...)


How to use:

- Put it into the libraries folder and load it as you would any library.

- Instantiate it

- Call the config() method.
This method accepts 5 arguments:

- query type: can be either 'user' for getting a user's timeline or 'search' for searching twitter for any term. advanced queries like '#foo+OR+bar&from;:n00b' can also be used.

- query string: in other words, what are you searching for.
for query type 'user', supply username in this field
for query type 'search', supply any searching terms you need

- number of tweets this class should return: Integer (defaults to 20)

- cache file lifespan. in seconds.(defaults to 5min)

- how many tweets will be saved to the chache file. Integer (defaults to 50)

Then use the method filter() with no parameters. It returns an array with the tweets, so be sure to call it assigning it's value to a variable, so you can pass it on to your view.

Aside from the ['time'] key and the ['time_ago'] key that are created inside this class, the returned array key values are the same as twitter's json response. That can be easily checked by installing Json View add-on to firefox and performing a query manually, like that:

For user timeline: http://api.twitter.com/1/statuses/user_timeline.json?id=username
For normal search: http://search.twitter.com/search.json?q=foo


With that information in hands, you can easily call a foreach in your view and retrieve only the info you want from the tweets.

For now, there's a known issue i didn't have the time to hunt down: If you set the class to get more tweets than the query itself returns, it seems to start looping through the result from the beginning, until it reaches the number you defined.

I've attached the php file to this post, so you can download and try it.

And that's it. I hope it helps someone.
And any feedback is much appreciated.
Also, feel free to extend it to fulfill your needs.
#2

[eluser]Bartolo![/eluser]
i didnt get it work.

and when i try http://api.twitter.com/1/statuses/user_timeline.json?id= the website of twitter asks for a username and password. Does this mean that this class isnt working any more???

regards,

Bart

Code:
function index()
    {    
        $this->load->library('Tbird');
        
        $this->tbird->config('user', 'myusername', '5', '300', '25');
        
        $data['tweets'] = $this->tbird->filter();
        
        $this->load->view('v_home', $data);
    }


gives:

Code:
<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined index:  results</p>
<p>Filename: libraries/Tbird.php</p>
<p>Line Number: 268</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  array_slice() expects parameter 1 to be array, null given</p>
<p>Filename: libraries/Tbird.php</p>
<p>Line Number: 273</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>

<p>Message:  Invalid argument supplied for foreach()</p>
<p>Filename: libraries/Tbird.php</p>
<p>Line Number: 276</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined variable: data</p>
<p>Filename: libraries/Tbird.php</p>

<p>Line Number: 288</p>
#3

[eluser]Unknown[/eluser]
Bart, try instantiating it before using it.
Instead of
Code:
function index()
    {    
        $this->load->library('Tbird');
        
        $this->tbird->config('user', 'myusername', '5', '300', '25');
        
        $data['tweets'] = $this->tbird->filter();
        
        $this->load->view('v_home', $data);
    }
Try using it like that:
Code:
function index()
    {    
        $this->load->library('Tbird');
        
        $instance = new Tbird();

        $instance->config('user', 'myusername', '5', '300', '25');
        
        $data['tweets'] = $instance->filter();
        
        $this->load->view('v_home', $data);
    }

The errors you are getting are because you did not instantiate a Tbird object, without an object you can't set config, and the class by itself will return nothing.

I hope that solves your problem.
#4

[eluser]Bartolo![/eluser]
Thanks a lot man!

That indeed worked for me. I thought libraries where automatically instantiated...

edit:

i try to convert the date but i can't get it work. do you know how to convert the unix date to YY-MM-DD ??

when i use date(); php gives the error: second parameter (date string) is to long.

Thanks in advance!
#5

[eluser]Bartolo![/eluser]
I'm sorry i bother you again, but it finally worked...until this morning. Everything is still the same but i don't get any results any more:

Code:
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined index:  results</p>
<p>Filename: libraries/Tbird.php</p>
<p>Line Number: 268</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  array_slice() expects parameter 1 to be array, null given</p>
<p>Filename: libraries/Tbird.php</p>
<p>Line Number: 273</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>

<p>Message:  Invalid argument supplied for foreach()</p>
<p>Filename: libraries/Tbird.php</p>
<p>Line Number: 276</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined variable: data</p>
<p>Filename: libraries/Tbird.php</p>

<p>Line Number: 288</p>

</div>

I don't get it...you?

EDIT: Now it's working again...i did nothing...very strange...
#6

[eluser]Unknown[/eluser]
I am getting the same errors.

I have been looking at a couple of Twitter libraries for Codeigniter and am struggling to find something which offers very basic functionality. Literally all I want to do is display the most recent tweet from a specified user.

The one mentioned on this thread generates errors and the Elliot Haughin library looks so powerful that I am struggling to get it to do something very simple. It prompts the user to authenticate themselves on Twitter. That's not what I want at all. I just want to pass in a user id into a function and retrieve the most recent tweet.

Nothing fancy, no need for users to actually tweet using my website.

Any suggestions?
#7

[eluser]Unknown[/eluser]
I know this a really old post, and that i might be blind - but where is the attachment ? I did like to look into this code as this is the only one that meets my requirements when i search the forum




Theme © iAndrew 2016 - Forum software by © MyBB