如何让twitter tools for wordpress推送bit.ly

上次我们说到了如何让twitter tools推送tinyurl,但是现在bit.ly已经是第一大tinyurl供应商了,所以我们也全面转向bit.ly吧,代码如下

在theme的functions.php的开头增加以下代码:

//start bit.ly

add_filter('tweet_blog_post_url', 'make_bitly_url');
if (!function_exists(’make_bitly_url’)) {
function make_bitly_url($url,$login = 'YOUR_USER_NAME',$appkey = 'YOUR_API_KEY',$format = 'xml',$version = '2.0.1')
{
//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 $shorturl="http://bit.ly/".$xml->results->nodeKeyVal->hash;
}
}
}

演示:http://google.org.cn @guao

via davidwalsh

This entry was posted in 我的工作学习, 电脑相关 and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


three × = 24

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>