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 bit.ly. 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 bit.ly url shortening API request by adding below piece of code.


$url=make_bitly_url($url);


/* make a URL small */
function make_bitly_url($url,$format = 'xml',$version = '2.0.1')
{
$login=""; //your bit.ly username
$appkey=""; //your bit.ly API key. You can freely get it from bit.ly
//create the URL
$bitly = 'http://api.bit.ly/shorten?version='.$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 'http://bit.ly/'.$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 bit.ly 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