最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP通過調(diào)用新浪API生成t.cn格式短網(wǎng)址鏈接的方法詳解

 更新時間:2019年02月20日 10:54:58   作者:大象已無形  
這篇文章主要介紹了PHP通過調(diào)用新浪API生成t.cn格式短網(wǎng)址鏈接的方法,結(jié)合實例形式較為詳細的分析了php調(diào)用新浪API生成t.cn格式短網(wǎng)址鏈接的具體操作步驟與相關(guān)注意事項,需要的朋友可以參考下

本文實例講述了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://m.fzitv.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格式

<?xml version="1.0" encoding="UTF-8"?><urls>
  <url>
    <url_short>http://t.cn/RBclsRo</url_short>
    <url_long>http://m.fzitv.net/</url_long>
    <type>0</type>
  </url></urls>

生成的短鏈接為 http://t.cn/RBclsRo ,訪問會跳轉(zhuǎn)到 http://m.fzitv.net/

完整調(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://m.fzitv.net/';<br>$data = getSinaShortUrl($source, $url_long);
print_r($data);<br>// 多個鏈接轉(zhuǎn)換
$url_long = array('http://m.fzitv.net/','http://m.fzitv.net/','http://m.fzitv.net/');
$data = getSinaShortUrl($source, $url_long);
print_r($data);
?>

輸出:

Array(
    [0] => Array
        (
            [url_short] => http://t.cn/RBclsRo
            [url_long] => http://m.fzitv.net/
            [type] => 0
        )
)Array(
    [0] => Array
        (
            [url_short] => http://t.cn/RBclsRo
            [url_long] => http://m.fzitv.net/
            [type] => 0
        )
    [1] => Array
        (
            [url_short] => http://t.cn/RBclsRo
            [url_long] => http://m.fzitv.net/
            [type] => 0
        )
    [2] => Array
        (
            [url_short] => http://t.cn/RBclsRo
            [url_long] => http://m.fzitv.net/
            [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è)計有所幫助。

相關(guān)文章

最新評論

香港| 昌江| 阿图什市| 梨树县| 桃园县| 崇信县| 阿图什市| 邵武市| 天全县| 方山县| 五指山市| 龙山县| 兴化市| 应城市| 东乡| 崇文区| 崇信县| 锡林浩特市| 昌黎县| 卢龙县| 朝阳区| 电白县| 大庆市| 清丰县| 巨鹿县| 玛曲县| 鹰潭市| 遂宁市| 金华市| 融水| 丹寨县| 屏边| 西贡区| 建始县| 唐河县| 章丘市| 九龙县| 钟山县| 祁连县| 二手房| 陕西省|