Skip to content

Accessing Twitter via Zend_Service_Twitter

Dear Reader,

Ok, I know there are a lot of OAuth haters out there and maybe it is not the easiest protocol to work with but really, I was able to get connected and running in about 15 minutes. (and that included 5 minutes of Google time)

The Project

I am playing around with Klout’s API and wanted to pull in some info from twitter to augment the Klout info I was seeing. Klout is easy – as just about every Mashery API I’ve worked with has been – you just register for a key and they make your calls. Twitter however, requires OAuth. I knew the Zend_Service_Twitter had recently undergone a revamp to support OAuth but there was one small problem. The script I am playing with is all backend. There is no front end at all. It just collects info and stores it in a database so I can query it later. (I’m an old-school database guy and love just writing ad-hoc queries to see what I can see) Twitter wants to redirect you to a site once you have authorized access. Since I don’t actually have a site to redirect it to, this was a problem.

The Solution – in two parts.

Ok, second things first, this script is straight procedural code at the moment. It isn’t even good enough to qualify as prototype code, more proof-of-concept. There’s no way in heck I’m sharing it with anyone so don’t even ask. I know, however that if things progress as I expect them to, the script will end up as a Zend Framework application. For that reason I wanted to use Zend_Service_Twitter. A quick Google showed me that my good friend and Zend Framework community member Michelangelo van Dam had a tutorial already written on this very subject titled Single User Zend_Service_Twitter. Part 2 is solved because everything I need to know is in that tutorial. So I move on to the first problem, how to get a valid OAuth token and secret. It turns out, that wasn’t hard at all either, it just required a little imagination.

Jaisen Mathai wrote up a blog post titled Twitter, PHP and OAuth a little while back explaining OAuth. In it he links to a quick piece of sample code that he put together to showcase his Twitter API wrapper. The wrapper itself is good and if I had not already decided to go with Zend Framework, I would have used it. The sample code however, allowed me to authenticate with twitter and then store the token and secret, the two things you must have to make this work. So I took his sample code, put it on my development server, followed his instructions to the letter and voilà, I had my token and and token secret.

I then went back to Dragonbe’s blog post, grabbed his code, modified it slightly so that I didn’t have to use Zend_Config and it worked too!

Conclusion

If you want to get started with Twitter’s OAuth, the two tutorials I’ve linked to are great jumping off points. Enjoy!

Until next time,
I <3 |<
=C=

Photo Credit:erikeldridge

Win free SQLYog!

Dear Reader,

[UPDATE: This contest is now over. Thanks to my friends at SQLYog for the licenses and congratulations to the winners.]

My buddies over at SQLYog asked me the other day what they could do to help spread the word about the Enterprise version fo SQLyg and how cool it is to the PHP community. (For a detailed description of how cool it is, see my recent post “SQLYog – A Superficial Review“) I told them what I tell everyone, free is good!
(more…)

Rise of the Spewbots

Dear Reader,

More specifically,

Dear reader thinking that it is a good idea to write a twitter bot that automatically spews tweets without regard to anyone else because everyone obviously wants to know what your bot has to say,

I guess because Oprah is now on twitter, things are exploding. Today alone, I’ve noticed 5-7 new bots on twitter polluting my search time lines spouting drivel and polluting them to the point of almost uselessness.

How I use twitter

I guess first, I should explain how I use twitter since different people use it differently. One of my jobs at Ibuildings is to be a PHP Community Evangelist. It’s the awesome part of my job and I love it. My twitter persona revolves around that aspect of my life. I try to make most of my tweets valuable to at least someone. (ok, my recent rants against #skype were just therapeutic for me.) My general rule of thumb is that most of my tweets need to inform or amuse.

I like finding people asking questions tagged #php and helping by answering them when I can. I like finding new blog posts about #php and helping spread the good ones. The rise of using search and hashtags has really expanded the usefulness of twitter for me…until now.

Rise of the Spewbots

Over the past few months I’ve seen an increasing number of bots tweeting. I have coined the term spewbots because for the most part, these bots spew forth data that I can easily get elsewhere should I want it and they add very little to the conversation. These bots break down into four categories.

Accidental spewbots

Today I got 6 svn check in notices from a spewbot. Now honestly, I’m sure they weren’t trying to pollute anyone’s time line. However, because they use the Zend Framework, I get their notices. I’ll refrain from ranting about the stupidity of tweeting svn check in notices, if that’s your thing and it doesn’t affect me then hey, go for it. Please let me make one request though. Protect your account’s timeline. A simple check of a box on your account’s settings and your tweets won’t go out into the public time line. Anyone who wants can follow your bot and once you approve them, can can partake of your spewy goodness.

Hashtag repeaters

I really don’t understand why some people feel the need to automatically re-tweet things that people tag. @hashwordpress is an excellent example of this. If anyone wants every tweet that has the #wordpress hashtag, all they need to do use search.twitter.com, twitterfall.com or any of a hundred ways to get these tweets; re-tweeting them helps no one.

Can we please just get rid of these? If you are the owner of one of these bots, please either shut it down, protect it’s time line so that you are not a spewbot, or leave me a comment here telling me why I’m wrong and they are really a good thing.

Bot testing spewage

All of a sudden on Friday, my entire twitter time line was replaced by a series of tweets from a single account. Each of them were just random words and the #php hashtag. In my frustration, I tweeted to this bot to please stop polluting my time line. Much to my surprise, I got an email about 20 minutes later from the owner of the bot asking what the problem was. I explained to them that their testing was polluting the #php hashtag. I asked them to replace the # with another character and they wouldn’t be bothering anyone. Much to my surprise, they agreed and I haven’t seen a tweet from them again.

If you are testing, there is no need to tag your tests. You are monitoring them and can tell if your bot is working. If you refuse to protect your bot’s time line during testing at least don’t tag the tweets, please?

…and the rest

There’s not really a way to classify the rest of the spewbots. Most of them that pollute the #php time line are job related. If they only tweeted one or two tweets a day it probably wouldn’t annoy me so much but you get bots like @fresh_projects, @freelance_jobs and @joomlajobs that tweet multiple tweets at a time several times a day.

I really don’t have a good way to handle these. To be honest, they are like like Democrats, they piss me off but they’ve got as much right to be here as I do. I really wish my client of choice, twhirl, would let me right click on an account and tell it to never ever show me a tweet from this account again. That way they are free to pollute, but I am free to ignore them. Actually, this would be an awesome feature to implement in twitter itself but since it takes 30 days to get a response from them on a simple bug report/request these days, the chances of that happening are slim. So for now, I’ll just have to put up with them.

If anyone has a good idea for how to cleanse the #php search timeline of these parasites, I’d love to hear it. I tried building a Y!Pipes filter to filter them out but Pipes won’t check the feed often enough to pickup all the tweets.

Conclusion

Until we find a permanent solution to the problem of spewbots, I’ guess we just have to put up up with them. I filter the most egregious of the spewbots with the -from: option in search.twitter.com but there’s a 140 limit to the search query so I have to constantly have to re-evaluate which ones I filter and which ones I put up with.

I’m open to ideas on how to combat the problem of spewbots, especially the last group of them. For now though, I’m out of ideas.

Until next time,
(l)(k)(bunny)
=C=

One Line Linux “Twitter From File” Command

Dear Reader,

Sometime recently I was surfing around and came across a blog post where a user wrote a PHP script – fully OO, it was very pretty – that would pop the top line off of a file and tweet it, that struck me as odd. Actually, first it struck me as stupid since all twitter.com needs is another mindless bot spewing lines from a file at regular intervals, once I got past stupid it struck me as odd.
Update: ‘tweetFromFile’ PHP Class is the original blog post.

I used to have a friend named Michael Chaney. (I’m pretty sure he and I are friends but we’ve not spoken since June.) Michael once posted in an email that one of his hobbies/quirks was that he would try and find ways to condense complex tasks into a single line of bash script. (I’m paraphrasing) I thought of that email when I read the “Tweet a line from a file” post. It just strikes me that it’s a bit overkill to fire up PHP for a simple task like this.

Before you ask, no, I’m not falling out of love with PHP but it’s a “right tool for the job” issue. PHP can be used to do this, but it’s not really necessary. So, if we are going to ignore the web’s all-purpose sledge hammer, what can we use? How about the tools that come with Linux?

Source Material

First, you are going to need a file to tweet. Now if you are using this to do something stupid like tweet UTC every minute or the status changes of your dorm room lamp then stop right now. I do not want the powers I’m going to teach you used for evil. However, if you are planning something like tweeting the “Dead Parrot” skit one line at a time, read on my friend and make sure you ping me so I can follow you. Either way you will need a file full of single lines less than 140 characters each. For my testing, I started with a Unix fortune file “bofh-excuses”

If you are on CentOS?

yum install fortune-bofh-excuses

If you are not on CentOS, figure it out on your own.

I’ll leave it as an exercise for the user to figure out how to strip out the cruft from the file or just create your own file of wit ready for tweeting. Whatever you do, name the file tweets.txt and put it in your working directory.

Twitter Account

Yes Sparky, you will need a twitter account to play with this code. No Sparky, you can’t borrow mine. If you don’t have one or you don’t want to bother your followers with inane test messages, I suggest registering a new one. Grab one nobody would want. (Not that I did, my test account is @elePHPant)

The Command

Here it is in all its glory for those too anxious to wait for my explanation.

head -n 1 tweets.txt | xargs -r -s 140 -I {} curl -s -d "status={}" -u twitterAcct:p.ass.word  http://twitter.com/statuses/update.json &gt; /dev/null ;sed -i '1d' tweets.txt

For those of you who see line noise from an old modem, let’s go through this line by line. Note: broken up like this is will not work, if you are copying and pasting, use the one above.

1
2
3
4
head -n 1 tweets.txt
xargs -r -s 140 -I {} 
curl -s -d "status={}" -u twitterAcct:p.ass.word  http://twitter.com/statuses/update.json &gt; /dev/null
sed -i '1d' tweets.txt
  1. This command returns the first line from the tweets.txt file.
  2. This command helps us build a command to execute using the output form line #1. The -I {} is critical here as it’s what tells xargs to take the input – in this case coming from stdin – and replace every instance of {} with it. For safety’s sake, the -s 140 makes sure we don’t send twitter anything over 140 characters. Finally, the -r makes sure that it doesn’t call curl if there is no line to pass in.
  3. This is the heart of the command, a call to curl.
    • -s, tells curl to run silent. This does not prevent output but it suppresses curl’s normal output. Anything coming from twitter will still be output.
    • The next option, -d specifies the string of data to be POSTed. Since we need this to be POST instead of GET, we have to specify the data string this way. The string following the -d command is the data to be sent. This is normal HTML name=value pairs separated by ampersands.
    • -u allows us to specify the username:password pair. Twitter uses basic authentication so it’s easy to authenticate simple tools like this. It is also highly insecure since you have to actually type your username and password into the cron script. This is one of these don’t try this at home things.
    • The next parameter is the url to call. If you have questions about how to call twitter’s API, check out the twitter API page for details.
    • Finally, because this command will return data, the final portion of this command will dump anything twitter sends back, in our case, JSON, to /dev/null. There is no error checking on this command, it either works and you see the tweet, or it fails and you don’t.
  4. This command pops the first line off the file using sed, the Serial EDitor. The -i command tells sed to edit the file in place. the ‘1d’ says delete the first line. The final parameter to the sed command is the name of the file.

That’s it, drop all of those into a cron job that runs every 10 minutes, place 100 lines tweets.txt that advertise your blog, weight loss, male enhancement or a porn site and you too can reduce your followers to just the other bots who are following you in hopes that you will follow them so they can spam you.

I really don’t expect anyone to find this useful but it was an interesting exercise in bash, a tool I don’t get to use nearly as much as I would like to.

Until next time,
(l)(k)(bunny)
=C=

p.s. If you don’t follow me on twitter and want to, I’m a real person, not a bot. Follow me at @calevans.

Some Related Content:

Five Twitter “Rules of Engagement”

Dear Reader,

Of late I’ve watched several companies start to use twitter without a clue or a strategy. Here are my Five Twitter Rules of Engagement that will help companies get the most value out of twitter. (or at the very least keep them from embarrassing themselves.)

  1. Do not ask for advice that you are not willing or unable to act on.
    I see a lot of companies asking “Tell us how we can make it better”. If you say that, really mean it. If you ask me how to improve your cell-phone design and I tell you it needs to be 1/2 its current size for me to like it, don’t argue with me about it, you asked my opinion. If you can’t make it’s 1/2 the size or worse yet, if you have no intention of making substantial changes in the product based on solicited feedback, just don’t ask.
  2. Be sincere
    This is closely related to #1. When someone criticizes your product, sincerely thank them for taking the time to review your product. Even if all they say is “I f’n hate product X” At least they took the time to look at your product. If you really want to know why, ask them but ask sincerely. If you know why and are already fixing it, let them know. If you know why and can’t/won’t fix it, ignore them. You will not change their mind by arguing for your product in 140 chr. bites. Just let it go, you can’t please everyone.
  3. Engage with a purpose
    Again, closely linked to point #1, make sure that you have a reason to engage someone publicly before engaging. Defend your product and its honor when necessary. However, don’t feel the need to engage or even acknowledge every public tweet. Engage someone when you can make a positive difference in the situation.
  4. Do not argue
    Nobody who seriously wants to engage with your company is interested in arguing with you. If you argue publicly there are really only two outcomes.
    1. The person you are engaging bests you and you look like a fool
    2. You best person you are engaging, they feel like a fool, and they never do business with you.

    If I am your customer and I say your product sucks, don’t argue the point with me. Obviously, I have a reason for thinking this and you need to engage me constructively to find that reason and see if you can correct it.

  5. Move it off-line quickly
    This one is the most important of them all. When someone says that your customer service sucks, don’t publicly acknowledge it, privately acknowledge it and ask how you can help. If you can’t find an email address or IM account for the person, then publicly reply asking them to follow you so you can DM them. Get it out of the public timeline and engage the person one-on-one. If you can solve the problem, do so. Once it is completely solved and you’ve made a new friend, then if you are comfortable with the person, ask them to tweet that they got the problem resolved. Chances are, they are so surprised that they have already done this but you need some closure on the issue publicly. If they don’t feel comfortable announcing the resolution then you can but it has more impact if they do it.

None of this is rocket science so it always astounds me that companies don’t know this stuff. PR departments already know these things but the line level employees don’t. Here’s a clue, the responses from line-level employees are much more effective than the ones being regurgitated out of the PR/Marketing department. Don’t try and clamp down on these people, train them, teach them how to respond and teach them who to go to with issues. Most importantly, teach them these rules of engagement.

Until next time,
(l)(k)(bunny)
=C=