Api 在发送到Twitter之前缩短链接

Api 在发送到Twitter之前缩短链接,api,twitter,hyperlink,Api,Twitter,Hyperlink,我有一个PHP循环,显示不同的标题及其链接。我想通过Bitly自动生成的短链接将这些链接发送到Twitter。以下是链接的结构: <a href="http://twitter.com/home?status=This is the headling {dynamic Permalink}">Share on Twitter</a> 在循环创建了实际的永久链接之后,如何创建缩短的链接?您需要在bit.ly注册并获取API密钥 function make_bitly_

我有一个PHP循环,显示不同的标题及其链接。我想通过Bitly自动生成的短链接将这些链接发送到Twitter。以下是链接的结构:

<a href="http://twitter.com/home?status=This is the headling {dynamic Permalink}">Share on Twitter</a>


在循环创建了实际的永久链接之后,如何创建缩短的链接?

您需要在bit.ly注册并获取API密钥

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
现在,$login变量是您的login,$apeKey是您的apeKey,$url是长url,您希望变短,函数输出短的bit.ly地址


更多信息请访问:

谢谢你的帮助,迈克。不过,我不太确定如何将您提供的代码集成到我的实际页面中。你愿意帮我举一个完整的例子吗?集成非常简单。您只需将此函数添加到代码顶部的某个位置,然后当您想要使用它时,只需像这样使用它:make_bitly_url($url,$login,$apiKey);