本文實例講述了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)用時分配的AppKey,調(diào)用接口時代表應(yīng)用的唯一身份。
url_long 需要轉(zhuǎn)換的長鏈接,需要URLencoded,最多不超過20個。
多個url參數(shù)需要使用如下方式請求:url_long=aaa&url_long=bbb
創(chuàng)建source方法
1.進(jìn)入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 = 'http://www.zmynmublwnt.cn/'; $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格式
<?xml version="1.0" encoding="UTF-8"?><urls> <url> <url_short>http://t.cn/RBclsRo</url_short> <url_long>http://www.zmynmublwnt.cn/</url_long> <type>0</type> </url></urls>
生成的短鏈接為 http://t.cn/RBclsRo ,訪問會跳轉(zhuǎn)到 http://www.zmynmublwnt.cn/
完整調(diào)用方法如下:
<?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){<br> return false; } // 參數(shù)處理,字符串轉(zhuǎn)為數(shù)組 if(!is_array($url_long)){<br> $url_long = array($url_long); } // 拼接url_long參數(shù)請求格式 $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>// 單個鏈接轉(zhuǎn)換 $url_long = 'http://www.zmynmublwnt.cn/';<br>$data = getSinaShortUrl($source, $url_long); print_r($data);<br>// 多個鏈接轉(zhuǎn)換 $url_long = array('http://www.zmynmublwnt.cn/','http://www.zmynmublwnt.cn/','http://www.zmynmublwnt.cn/'); $data = getSinaShortUrl($source, $url_long); print_r($data); ?>
輸出:
Array(
[0] => Array
(
[url_short] => http://t.cn/RBclsRo
[url_long] => http://www.zmynmublwnt.cn/
[type] => 0
)
)Array(
[0] => Array
(
[url_short] => http://t.cn/RBclsRo
[url_long] => http://www.zmynmublwnt.cn/
[type] => 0
)
[1] => Array
(
[url_short] => http://t.cn/RBclsRo
[url_long] => http://www.zmynmublwnt.cn/
[type] => 0
)
[2] => Array
(
[url_short] => http://t.cn/RBclsRo
[url_long] => http://www.zmynmublwnt.cn/
[type] => 0
)
)
經(jīng)測試,這個生成接口還是比較穩(wěn)定的!