在當今社交媒體時代,短鏈接已經逐漸成為人們發送和分享信息的重要方式之一。而騰訊短鏈接作為國內短鏈接服務的領軍品牌,擁有著極大的影響力和使用率。本文將從多個方面對騰訊短鏈接進行詳細的解析。
一、騰訊短連接介面
騰訊短連接介面包括:生成短連接介面、獲取短連接真實url介面、長鏈接轉短鏈接介面、短鏈接還原介面。其中,生成短連接介面是騰訊短鏈接的核心功能之一。下面是調用生成短鏈接介面的代碼示例:
<?php $access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $url_long = 'https://www.qq.com'; $api_url = 'https://api.weixin.qq.com/cgi-bin/shorturl?access_token='.$access_token; $post_arr = array(); $post_arr['action'] = 'long2short'; $post_arr['long_url'] = $url_long; $ch = curl_init();//初始化curl curl_setopt($ch, CURLOPT_URL,$api_url);//設置url curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//設置獲取的信息以文件流的形式返回,而不是直接輸出。 curl_setopt($ch, CURLOPT_POST, true);//post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_arr));//設置post參數 $result = curl_exec($ch);//執行curl curl_close($ch);//關閉curl $result_arr = json_decode($result, true); ?>
通過調用這個介面,用戶可以將長鏈接轉化為短鏈接,方便在微信等社交媒體上分享和使用。
二、騰訊短鏈接url生成介面
騰訊短鏈接url生成介面是快速生成騰訊短鏈接的一種在線工具。用戶只需要輸入原始長鏈接,點擊「生成」按鈕,即可快速獲得對應的騰訊短鏈接。下面是騰訊短鏈接url生成介面的代碼示例:
<form action="https://url.cn/5HepkpR" method="post"> <input type="text" name="long_url" placeholder="請輸入要轉換的長鏈接" /> <input type="hidden" name="appid" value="xxxxx" /> <input type="hidden" name="appkey" value="xxxxx" /> <input type="submit" value="生成" /> </form>
通過這個介面,用戶可以快速方便地獲得騰訊短鏈接。
三、騰訊短鏈接在線生成器
除了騰訊短鏈接url生成介面,騰訊也提供了一個在線生成器,在線生成短鏈接。下面是騰訊短鏈接在線生成器的代碼示例:
<form id="input_form"> <input id="input_long_url" type="text" value="" placeholder="輸入長網址,按Enter縮短" /> </form> <div id="output_area"> <label for="output_short_url">短地址:</label> <input type="text" id="output_short_url" size="50px"> </div> <script type="text/javascript"> $('#input_long_url').enterKey(function () { if (isUrl($('#input_long_url').val())) { var url_long_original = $('#input_long_url').val(); $('#input_long_url').val(''); $.ajax({ type: "POST", url: '/api/shorten', data: {longurl:url_long_original}, dataType: "json", success:function(response) { if (!!response.url_short) { $('#output_short_url').val(response.url_short); } else { alert(response.msg); } } }); } else { alert('請輸入有效的鏈接'); } }); </script>
這個在線生成器為用戶提供了一種更加方便和易於使用的方式生成騰訊短鏈接。
四、騰訊短鏈接url代碼
在一些特定的場景下,用戶需要手動添加騰訊短鏈接到自己的網頁或APP中。這就需要用戶獲得騰訊短鏈接的url代碼。代碼示例如下:
<a href="https://url.cn/5JvcsFg" target="_blank">這裡是鏈接文字</a>
用戶只需要將上面的代碼嵌入到自己的網頁或APP中即可使用騰訊短鏈接。
五、騰訊短鏈接url介面
騰訊短鏈接url介面是騰訊短鏈接的另一種在線工具。用戶可以通過這個介面根據長鏈接獲取對應的騰訊短鏈接。下面是騰訊短鏈接url介面的代碼示例:
GET https://url.cn/5qrnNtS?target=url&appid=345&traceid=123456789
通過這個介面,用戶可以實時獲取對應的騰訊短鏈接,便於使用和分享。
六、騰訊短鏈接快捷指令
除了上述常規的生成騰訊短鏈接的方式,用戶還可以通過一些快捷指令來實現更加方便快捷地生成騰訊短鏈接。下面是一個使用Python實現的快捷指令的代碼示例:
#!/usr/bin/env python # coding:utf-8 import os import sys import requests import pyperclip LONG_TURL = 'http://api.weibo.com/2/short_url/shorten.json?source=4180299053&url_long=%s' def main(): url = sys.argv[1] if len(sys.argv) >= 2 else pyperclip.paste() if not url.startswith('http'): url = 'http://' + url result = requests.get(LONG_TURL % url).json()[0] if result.has_key("error_code"): print result["error"] else: pyperclip.copy(result['url_short']) print result['url_short'] os.system('open %s' % result['url_short']) if __name__ == '__main__': main()
通過這個快捷指令,用戶可以更加方便地將長鏈接轉化為騰訊短鏈接。
七、新浪短鏈接
除了騰訊短鏈接,國內還有另外一種常用的短鏈接服務,那就是新浪短鏈接。下面是一個調用新浪短鏈接生成介面的代碼示例:
<?php $url = $_GET['url']; $url_sina = 'http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long='.$url; $arr= json_decode(file_get_contents($url_sina),true); echo $arr[0]['url_short']; ?>
雖然新浪短鏈接使用率較高,但和騰訊短鏈接相比,其短鏈接的穩定性和形式更加單一,因此騰訊短鏈接的綜合優勢還是相對較為明顯的。
原創文章,作者:WNMC,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/137932.html