本文實例講述了PHP通過調用新浪API生成t.cn格式短網址鏈接的方法。分享給大家供大家參考,具體如下:
新浪提供了長鏈接轉為短鏈接的API,可以把長鏈接轉為 t.cn/xxx 這種格式的短鏈接。
API:
http://api.t.sina.com.cn/short_url/shorten.json (返回結果是JSON格式)
http://api.t.sina.com.cn/short_url/shorten.xml (返回結果是XML格式)
請求參數:
source 申請應用時分配的AppKey,調用接口時代表應用的唯一身份。
url_long 需要轉換的長鏈接,需要URLencoded,最多不超過20個。
多個url參數需要使用如下方式請求:url_long=aaaurl_long=bbb
創(chuàng)建source方法
1.進入http://open.weibo.com/ ,選擇菜單 微連接->網站接入。
2.點擊立即接入,創(chuàng)建新應用,隨便填寫應用名稱,點擊創(chuàng)建。
3.創(chuàng)建成功后,AppKey就是source參數的值,可以用于請求創(chuàng)建短鏈接。
測試代碼:
?php
$api = 'http://api.t.sina.com.cn/short_url/shorten.json'; // json
// $api = 'http://api.t.sina.com.cn/short_url/shorten.xml'; // xml
$source = '您申請的AppKey';
$url_long = 'https://www.jb51.net/';
$request_url = sprintf($api.'?source=%surl_long=%s', $source, $url_long);
$data = file_get_contents($request_url);
echo $data;
?>
返回JSON格式
[
{
"url_short": "http:\/\/t.cn\/Rki0twp",
"url_long": "http:\/\/www.cnblogs.com\/daxiangxm",
"type": 0
}
]
返回XML格式
?xml version="1.0" encoding="UTF-8"?>urls>
url>
url_short>http://t.cn/RBclsRo/url_short>
url_long>https://www.jb51.net//url_long>
type>0/type>
/url>/urls>
生成的短鏈接為 http://t.cn/RBclsRo ,訪問會跳轉到 https://www.jb51.net/
完整調用方法如下:
?php/**
* 調用新浪接口將長鏈接轉為短鏈接
* @param string $source 申請應用的AppKey
* @param array|string $url_long 長鏈接,支持多個轉換(需要先執(zhí)行urlencode)
* @return array
*/function getSinaShortUrl($source, $url_long){
// 參數檢查
if(empty($source) || !$url_long){br> return false;
} // 參數處理,字符串轉為數組
if(!is_array($url_long)){br> $url_long = array($url_long);
} // 拼接url_long參數請求格式
$url_param = array_map(function($value){
return 'url_long='.urlencode($value);
}, $url_long);br> $url_param = implode('', $url_param);
// 新浪生成短鏈接接口
$api = 'http://api.t.sina.com.cn/short_url/shorten.json'; // 請求url
$request_url = sprintf($api.'?source=%s%s', $source, $url_param); br> $result = array(); // 執(zhí)行請求
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $request_url); br> $data = curl_exec($ch);br> if($error=curl_errno($ch)){br> return false;
}
curl_close($ch); $result = json_decode($data, true); return $result;
}
//AppKey br>$source = '您申請的AppKey';br>// 單個鏈接轉換
$url_long = 'https://www.jb51.net/';br>$data = getSinaShortUrl($source, $url_long);
print_r($data);br>// 多個鏈接轉換
$url_long = array('https://www.jb51.net/','https://www.jb51.net/','https://www.jb51.net/');
$data = getSinaShortUrl($source, $url_long);
print_r($data);
?>
輸出:
Array(
[0] => Array
(
[url_short] => http://t.cn/RBclsRo
[url_long] => https://www.jb51.net/
[type] => 0
)
)Array(
[0] => Array
(
[url_short] => http://t.cn/RBclsRo
[url_long] => https://www.jb51.net/
[type] => 0
)
[1] => Array
(
[url_short] => http://t.cn/RBclsRo
[url_long] => https://www.jb51.net/
[type] => 0
)
[2] => Array
(
[url_short] => http://t.cn/RBclsRo
[url_long] => https://www.jb51.net/
[type] => 0
)
)
經測試,這個生成接口還是比較穩(wěn)定的!
PS:這里為大家推薦一款本站短網址生成工具(也是使用的第三方API接口生成的短網址)
短鏈(短網址)在線生成工具:
http://tools.jb51.net/password/dwzcreate
更多關于PHP相關內容感興趣的讀者可查看本站專題:《php curl用法總結》、《PHP網絡編程技巧總結》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《PHP數據結構與算法教程》及《PHP中json格式數據操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:- 一個php短網址的生成代碼(仿微博短網址)
- PHP生成短網址的3種方法代碼實例
- php簡單實現短網址(短鏈)還原的方法(測試可用)
- PHP生成短網址方法匯總
- PHP將URL轉換成短網址的算法分享
- PHP長網址與短網址的實現方法
- php生成短網址示例
- PHP生成短網址的思路以及實現方法的詳解
- PHP利用DWZ.CN服務生成短網址
- php 短鏈接算法收集與分析
- php調用新浪短鏈接API的方法
- php生成短網址/短鏈接原理和用法實例分析