本文介绍了发送到Twitter之前缩短链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个PHP循环显示不同的标题及其链接。我想这些链接发送到Twitter通过Bitly自动生成的短链接。这是链接是如何构成的:
I have a PHP loop that displays different headlines and their links. I'd like to send those links to Twitter with an auto-generated short link via Bitly. This is how the link is structured:
<a href="http://twitter.com/home?status=This is the headling {dynamic Permalink}">Share on Twitter</a>
如何创建一个链接缩短循环创造了实际的固定链接后?
How can I create a shortened link after the loop has created the actual permalink?
推荐答案
您需要在bit.ly注册并获得API密钥。
You need to register at bit.ly and get the API key.
function make_bitly_url($url, $login, $apiKey, $format = 'xml',$version = '2.0.1', $history=1 ) {
if(substr($url, 0, 7) != 'http://')
$url = 'http://'.$url;
$bitly = 'http://api.bit.ly/shorten';
$param = 'version='.$version.'&longUrl='.urlencode($url).'&login='
.$login.'&apiKey='.$apiKey.'&format='.$format.'&history='.$history;
//get the url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $bitly . '?' . $param);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
//parse depending on desired format
if(strtolower($format) == 'json') {
$json = @json_decode($response,true);
return $json['results'][$url]['shortUrl'];
} else {
$xml = simplexml_load_string($response);
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
} // end: function
现在的$登录变量是你登录,$ apeKey您的apiKey和$ URL是长的URL,你想成为短,功能输出短路bit.ly地址。
Now the $login variable is your login, the $apeKey your apiKey and $url is the long url, you want to be short and the function outputs the short bit.ly address.
在更多:
这篇关于发送到Twitter之前缩短链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!