Code snippet: Posting to Twitter with shortened bit.ly links
If you regularly post blog or news articles, and you have a Twitter account, you may tweet about each article once it's posted, but it would be much easier if a tweet was posted automatically once you post the article. Using the Twitter API with OAuth, and using shortened links using the bit.ly API, this can be achieved with PHP.
I found connecting to Twitter with OAuth to be fairly tricky, so I am going to recommend using someone else's script to do so. Joe Chung's post, on his blog “Nothing of Value”, is a great resource on how to connect to Twitter using OAuth. By downloading his files and following the instructions to set up a Twitter application, you will be ready to post to Twitter from your own web applications.
Note: The URL to creating a new Twitter application has now changed to https://dev.twitter.com/apps/new. One thing Joe's post doesn't mention is that you need to give your application Read & Write privileges, which you can do in the settings tab of your application.
Once you have your Twitter application working, you can use Joe's tweet.php file to send tweets to your Twitter timeline:
require_once("tweet.php");
$update = "My first twitter update!";
post_tweet(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $update,
ACCESS_TOKEN, ACCESS_TOKEN_SECRET, true, true);
We first include the file, then run the post_tweet() function with our Twitter update as the third parameter. The four constants used in the other parameters are defined in the globals.php file (which is in turn included in tweet.php).
These line of code should post a tweet on your timeline, but what if you want to link through to an article you have just created? Chances are the URL to your post will be pretty long, and that won't leave you many (if any) characters for the 140 character limit on Twitter. The solution to this is to use a URL shortening service. I'm going to use bit.ly as it has an easy to use API.
First you will need to register an account with bit.ly and retrieve your API key by visiting https://bitly.com/a/your_api_key. We then make a connection to bit.ly with our original link which will return the shortened URL:
$link = 'http://www.yourwebsite.com/with/a/really/long/url'; $bitly = 'http://api.bit.ly/v3/shorten?login=YOUR_USERNAME&apiKey=API_KEY&uri=' . urlencode($link).'&format=txt'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $bitly); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); $short = curl_exec($ch); curl_close($ch);
You obviously need to replace YOUR_USERNAME and API_KEY with their respective values from the link above. The variable $short will then contain your bit.ly shortened link.
We can then use this in conjunction with our earlier code to post it to Twitter:
$link = 'http://www.yourwebsite.com/with/a/really/long/url';
$bitly = 'http://api.bit.ly/v3/shorten?login=YOUR_USERNAME&apiKey=API_KEY&uri='
. urlencode($link).'&format=txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $bitly);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$short = curl_exec($ch);
curl_close($ch);
require_once("tweet.php");
$update = "My first twitter update! Read more at " . $short;
post_tweet(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $update,
ACCESS_TOKEN, ACCESS_TOKEN_SECRET, true, true);
If you want to include your article's title in the tweet, then it's likely that it will be dynamic and could once again push you over the character limit. To get round this, I worked out what I was going to say in my tweet and counted the characters. Here's what I post to Twitter when I add a new article:
New article "XXXXXXXXXX" posted. Read more http://bit.ly/XXXXXX
Not including the article title (but including the quotes), I have 53 characters. Taken off of 140, this leaves with 87 characters maximum for the article article. If we have to shorten to title, we can add a ... to the end of it, so our maximum limit is 84 characters plus the 3 character ellipsis. Assuming the variable $title is our dynamic article title (possibly posted from a form), we can shorten our title like so:
$title = strlen($title) > 87 ? substr($title, 0, 84) . '...' : $title;
Our final code will be:
$link = 'http://www.yourwebsite.com/with/a/really/long/url';
$bitly = 'http://api.bit.ly/v3/shorten?login=YOUR_USERNAME&apiKey=API_KEY&uri='
. urlencode($link).'&format=txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $bitly);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$short = curl_exec($ch);
curl_close($ch);
$title = strlen($title) > 87 ? substr($title, 0, 84) . '...' : $title;
require_once("tweet.php");
$update = 'New article "'.$title.'" posted. Read more at ' . $short;
post_tweet(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $update,
ACCESS_TOKEN, ACCESS_TOKEN_SECRET, true, true);
blog comments powered by Disqus