Friends
| Great PHP Twitter Library |
|
|
|
| Written by Nate Lyman |
| Monday, 15 February 2010 10:41 |
|
Tonight I was updating "That's What She Said" by having the site post new quotes to twitter in real time. In researching the Twitter API, I explored writing my own Twitter API Class. It would be easy enough but I decided to see what was out there first. I am extremely glad I did. Tijs Verkoyen has written an excellent Twitter API class, that will do pretty much anything you could ever want to do on Twitter. Not only is it well written, but it is equally well documented. After the jump, I'll dive deeper into the implementation.
Once you've downloaded the Twitter class and put it into your includes folder you can write a test script to do post a message to twitter. As you can see it is very simple to implement. Include it, pass in your Username and Password, and you're set. If you're going to automate tweets like I did you'll want to throw in some logic to automatically shorten URLs, as well as truncate text to keep you under 140 characters. The first thing I do is create a function where I pass in a message, as well as an ID. You can implement the function however you want, but this is how I decided to do it. You'll wan't to throw more error handling in there, I removed my error handling to condense the code. After we shorten the URL, we create an array of the Twitter Hash Tags we will want to use. Once we concatenate the link and hash tags we compute how many characters we have left to use for the actual message. If the entire message fits, we use it, if not, use the wordwrap function to break the message into pieces. I pick the first piece and concatenate the shortened message to the link and hash tags. There are more elegant ways to implement the message truncation, but this will work for basic purposes.
|




