api.php t.cn,PHP通过调用新浪API生成t.cn格式短网址链接的方法详解
本文實(shí)例講述了PHP通過調(diào)用新浪API生成t.cn格式短網(wǎng)址鏈接的方法。分享給大家供大家參考,具體如下:
新浪提供了長鏈接轉(zhuǎn)為短鏈接的API,可以把長鏈接轉(zhuǎn)為 t.cn/xxx 這種格式的短鏈接。
API:
http://api.t.sina.com.cn/short_url/shorten.json (返回結(jié)果是JSON格式)
http://api.t.sina.com.cn/short_url/shorten.xml (返回結(jié)果是XML格式)
請求參數(shù):
source 申請應(yīng)用時(shí)分配的AppKey,調(diào)用接口時(shí)代表應(yīng)用的唯一身份。
url_long 需要轉(zhuǎn)換的長鏈接,需要URLencoded,最多不超過20個(gè)。
多個(gè)url參數(shù)需要使用如下方式請求:url_long=aaa&url_long=bbb
創(chuàng)建source方法
1.進(jìn)入http://open.weibo.com/ ,選擇菜單 微連接->網(wǎng)站接入。
2.點(diǎn)擊立即接入,創(chuàng)建新應(yīng)用,隨便填寫應(yīng)用名稱,點(diǎn)擊創(chuàng)建。
3.創(chuàng)建成功后,AppKey就是source參數(shù)的值,可以用于請求創(chuàng)建短鏈接。
測試代碼:
$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.zyiz.net/';
$request_url = sprintf($api.'?source=%s&url_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格式
http://t.cn/RBclsRo
https://www.zyiz.net/
0
完整調(diào)用方法如下:
* 調(diào)用新浪接口將長鏈接轉(zhuǎn)為短鏈接
* @param string $source 申請應(yīng)用的AppKey
* @param array|string $url_long 長鏈接,支持多個(gè)轉(zhuǎn)換(需要先執(zhí)行urlencode)
* @return array
*/function getSinaShortUrl($source, $url_long){
// 參數(shù)檢查
if(empty($source) || !$url_long){
return false;
} // 參數(shù)處理,字符串轉(zhuǎn)為數(shù)組
if(!is_array($url_long)){
$url_long = array($url_long);
} // 拼接url_long參數(shù)請求格式
$url_param = array_map(function($value){
return '&url_long='.urlencode($value);
}, $url_long);
$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);
$result = array(); // 執(zhí)行請求
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $request_url);
$data = curl_exec($ch);
if($error=curl_errno($ch)){
return false;
}
curl_close($ch); $result = json_decode($data, true); return $result;
}
//AppKey
$source = '您申請的AppKey';
// 單個(gè)鏈接轉(zhuǎn)換
$url_long = 'https://www.zyiz.net/';
$data = getSinaShortUrl($source, $url_long);
print_r($data);
// 多個(gè)鏈接轉(zhuǎn)換
$url_long = array('https://www.zyiz.net/','https://www.zyiz.net/','https://www.zyiz.net/');
$data = getSinaShortUrl($source, $url_long);
print_r($data);
?>
輸出:
Array(
[0] => Array
(
[url_short] => http://t.cn/RBclsRo
[url_long] => https://www.zyiz.net/
[type] => 0
)
)Array(
[0] => Array
(
[url_short] => http://t.cn/RBclsRo
[url_long] => https://www.zyiz.net/
[type] => 0
)
[1] => Array
(
[url_short] => http://t.cn/RBclsRo
[url_long] => https://www.zyiz.net/
[type] => 0
)
[2] => Array
(
[url_short] => http://t.cn/RBclsRo
[url_long] => https://www.zyiz.net/
[type] => 0
)
)
經(jīng)測試,這個(gè)生成接口還是比較穩(wěn)定的!
PS:這里為大家推薦一款本站短網(wǎng)址生成工具(也是使用的第三方API接口生成的短網(wǎng)址)
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
總結(jié)
以上是生活随笔為你收集整理的api.php t.cn,PHP通过调用新浪API生成t.cn格式短网址链接的方法详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: “鸡眼”可以自己挖出来吗
- 下一篇: mysql error number 1