利用API實現(xiàn)短網(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)用時分配的AppKey,調(diào)用接口時代表應(yīng)用的唯一身份。url_long 需要轉(zhuǎn)換的長鏈接,需要URLencoded,最多不超過20個。

多個url參數(shù)需要使用如下方式請求:url_long=aaa&url_long=bbb

創(chuàng)建source方法

1.進入http://open.weibo.com/ ,選擇菜單 微連接->網(wǎng)站接入。2.點擊立即接入,創(chuàng)建新應(yīng)用,填寫應(yīng)用名稱,點擊創(chuàng)建。3.創(chuàng)建成功后,AppKey就是source參數(shù)的值,可以用于請求創(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://detail.tmall.com/item.htm?spm=a21wu.241046-us.9629632455.7.193eb6cbbC9gFg&id=585958323801';
$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://detail.tmall.com/item.htm?spm=a21wu.241046-us.9629632455.7.193eb6cbbC9gFg&id=585958323801",
"type": 0
}
]
返回XML格式
<?xml version="1.0" encoding="UTF-8"?>
<urls>
<url>
<url_short> http://t.cn/Rki0twp</url_short>
<url_long>https://detail.tmall.com/item.htm?spm=a21wu.241046-us.9629632455.7.193eb6cbbC9gFg&id=585958323801</url_long>
<type>0</type>
</url>
</urls>

生成的短鏈接為?http://t.cn/Rki0twp?,訪問會跳轉(zhuǎn)到?https://detail.tmall.com/item.htm?spm=a21wu.241046-us.9629632455.7.193eb6cbbC9gFg&id=585958323801

完整的類如下:

/*
* 生成新浪的短鏈接或還原新浪短鏈接
*/

class ShortUrl{
//新浪APPKEY
const APPKEY='xxxxxxxx'; //你申請的appkey
//CURL
private static function CURLQueryString($url){
//設(shè)置附加HTTP
$addHead=array("Content-type: application/json");
//初始化curl
$curl_obj=curl_init();
//設(shè)置網(wǎng)址
curl_setopt($curl_obj,CURLOPT_URL,$url);
//附加Head內(nèi)容
curl_setopt($curl_obj,CURLOPT_HTTPHEADER,$addHead);
//是否輸出返回頭信息
curl_setopt($curl_obj,CURLOPT_HEADER,0);
//curl_exec的結(jié)果返回
curl_setopt($curl_obj,CURLOPT_RETURNTRANSFER,1);
//設(shè)置超時時間
curl_setopt($curl_obj,CURLOPT_TIMEOUT,8);
//執(zhí)行
$result=curl_exec($curl_obj);
//關(guān)閉curl回話
curl_close($curl_obj);
return $result;
}
//處理返回結(jié)果
private static function doWithResult($result,$field){
$result=json_decode($result,true);
return isset($result[0][$field])?$result[0][$field]:'';
}
//獲取短鏈接
public static function getShort($url){
$url='http://api.t.sina.com.cn/short_url/shorten.json?source='.self::APPKEY.'&url_long='.$url;
$result=self::CURLQueryString($url);
return self::doWithResult($result,'url_short');
}
//獲取長鏈接
public static function getLong($url){
$url='http://api.t.sina.com.cn/short_url/expand.json?source='.self::APPKEY.'&url_short='.$url;
$result=self::CURLQueryString($url);
return self::doWithResult($result,'url_long');
}
}

你也可以用以下完整的方法

<?php
/**
* 調(diào)用新浪接口將長鏈接轉(zhuǎn)為短鏈接
* @param string $source 申請應(yīng)用的AppKey
* @param array|string $url_long 長鏈接,支持多個轉(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 = 'xxxxxxxssssss';
// 單個鏈接轉(zhuǎn)換
$url_long = 'https://detail.tmall.com/item.htm?spm=a21wu.241046-us.9629632455.7.193eb6cbbC9gFg&id=585958323801';
$data = getSinaShortUrl($source, $url_long);
print_r($data);

// 多個鏈接轉(zhuǎn)換
$url_longs = array(
'https://detail.tmall.com/item.htm?spm=a21wu.241046-us.9629632455.7.193eb6cbbC9gFg&id=585958323801',
'https://detail.tmall.com/item.htm?spm=a21wu.241046-us.9629632455.7.193eb6cbbC9gFg&id=585958323802',
'https://detail.tmall.com/item.htm?spm=a21wu.241046-us.9629632455.7.193eb6cbbC9gFg&id=585958323803',
'https://detail.tmall.com/item.htm?spm=a21wu.241046-us.9629632455.7.193eb6cbbC9gFg&id=585958323804',
'https://detail.tmall.com/item.htm?spm=a21wu.241046-us.9629632455.7.193eb6cbbC9gFg&id=585958323805',
);
$data_arr = getSinaShortUrl($source, $url_longs);
print_r($data_arr);
?>


以上是文章全部內(nèi)容



?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容