PHP通過調(diào)用新浪API生成t.cn格式短網(wǎng)址鏈接的方法詳解
本文實例講述了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.進入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.dbjr.com.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.dbjr.com.cn/</url_long> <type>0</type> </url></urls>
生成的短鏈接為 http://t.cn/RBclsRo ,訪問會跳轉(zhuǎn)到 http://www.dbjr.com.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.dbjr.com.cn/';<br>$data = getSinaShortUrl($source, $url_long); print_r($data);<br>// 多個鏈接轉(zhuǎn)換 $url_long = array('http://www.dbjr.com.cn/','http://www.dbjr.com.cn/','http://www.dbjr.com.cn/'); $data = getSinaShortUrl($source, $url_long); print_r($data); ?>
輸出:
Array(
[0] => Array
(
[url_short] => http://t.cn/RBclsRo
[url_long] => http://www.dbjr.com.cn/
[type] => 0
)
)Array(
[0] => Array
(
[url_short] => http://t.cn/RBclsRo
[url_long] => http://www.dbjr.com.cn/
[type] => 0
)
[1] => Array
(
[url_short] => http://t.cn/RBclsRo
[url_long] => http://www.dbjr.com.cn/
[type] => 0
)
[2] => Array
(
[url_short] => http://t.cn/RBclsRo
[url_long] => http://www.dbjr.com.cn/
[type] => 0
)
)
經(jīng)測試,這個生成接口還是比較穩(wěn)定的!
PS:這里為大家推薦一款本站短網(wǎng)址生成工具(也是使用的第三方API接口生成的短網(wǎng)址)
短鏈(短網(wǎng)址)在線生成工具:
http://tools.jb51.net/password/dwzcreate
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php curl用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》及《PHP中json格式數(shù)據(jù)操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- 一個php短網(wǎng)址的生成代碼(仿微博短網(wǎng)址)
- PHP生成短網(wǎng)址的3種方法代碼實例
- php簡單實現(xiàn)短網(wǎng)址(短鏈)還原的方法(測試可用)
- PHP生成短網(wǎng)址方法匯總
- PHP將URL轉(zhuǎn)換成短網(wǎng)址的算法分享
- PHP長網(wǎng)址與短網(wǎng)址的實現(xiàn)方法
- php生成短網(wǎng)址示例
- PHP生成短網(wǎng)址的思路以及實現(xiàn)方法的詳解
- PHP利用DWZ.CN服務(wù)生成短網(wǎng)址
- php 短鏈接算法收集與分析
- php調(diào)用新浪短鏈接API的方法
- php生成短網(wǎng)址/短鏈接原理和用法實例分析
相關(guān)文章
PHP curl 或 file_get_contents 獲取需要授權(quán)頁面的方法
本篇文章主要介紹了PHP curl 或 file_get_contents獲取需要授權(quán)頁面的方法,具有很好的參考價值。下面跟著小編一起來看下吧2017-05-05PHP數(shù)據(jù)庫編程之MySQL優(yōu)化策略概述
這篇文章主要介紹了PHP數(shù)據(jù)庫編程之MySQL優(yōu)化策略,簡單講述了mysql優(yōu)化的簡單技巧以及索引優(yōu)化、查詢優(yōu)化、存儲優(yōu)化等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08PHP中關(guān)鍵字interface和implements詳解
PHP 類是單繼承,也就是不支持多繼承,當一個類需要多個類的功能時,繼承就無能為力了,為此 PHP 引入了類的接口技術(shù)。下面這篇文章主要跟大家介紹了關(guān)于PHP中關(guān)鍵字interface和implements的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06