Thursday, September 9, 2010

Did Twitter make any change related to URL shortening?

I came to know that auto tweeting by cron jobs for our Twitter accounts were not working properly.

After doing some analysis I found that cron jobs were submitting the news successfully into our Social News site. Only tweeting part was getting failed. And this behaviour is inconsistent. i-e some news were tweeted to our Twitter accounts.

During my debugging process the failure occurs due to 140 characters limit. But previously this error didn't occur. Further troubleshooting revealed that automatic url shortening is not getting done. (I noticed that previously URLs were shortened using But I don't know how it was done without using any code for doing url shortening. I assumed that it was done automatically from Twitter)

Recently Twitter had shut down the basic authentication for its API.
I am not sure whether this behaviour change is due to this shut down.

Anyway, I have resolved this issue by explicitly calling url shortening API request by adding below piece of code.


/* make a URL small */
function make_bitly_url($url,$format = 'xml',$version = '2.0.1')
$login=""; //your username
$appkey=""; //your API key. You can freely get it from
//create the URL
$bitly = ''.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;

//get the url
//could also use cURL here
$response = file_get_contents($bitly);

//parse depending on desired format
if(strtolower($format) == 'json')
$json = @json_decode($response,true);
return $json['results'][$url]['shortUrl'];
else //xml
$xml = simplexml_load_string($response);
return ''.$xml->results->nodeKeyVal->hash;

Everything is working fine after adding above code. If you find any issue with our Tweets you can inform me.

And, it seems is imposing Rate limits. So, it may not work if the number of requests in particular hour exceeds the Rate Limit.

More Articles...
You can bookmark this blog for further reading, or you can subscribe to our blog feed.

No comments:

Search This Blog