新浪微博API生成短鏈接


通過新浪微博API,生成短鏈接,支持一次性轉多個長鏈接

什么是短鏈接

短鏈接,通俗來說,就是將長的URL網址,通過程序計算等方式,轉換為簡短的網址字符串。

短鏈接服務

國內各大微博都推出了自己的短鏈接服務。例如新浪微博、騰訊微博等。

為什么選用新浪微博API

  1. 新浪微博短鏈接API是開放的
  2. 新浪微博短鏈接API不需要用戶登錄

文檔查詢鏈接

使用方法

拿到自己的AppKey后,替換類的成員屬性$appKey的值即可,如下這樣的,$shortUrl是API請求地址


// APPkey,我在網上找的(https://fengmk2.com/blog/appkey.html),可以自己申請
protected $appKey = '569452181';
// 轉短連接API地址
protected $shortUrl = 'https://api.weibo.com/2/short_url/shorten.json?';

其他的,基本不需要配置,直接實例化類ShortLink,然后調用方法getShortUrl即可,需要說明的是長鏈接URL數組$longUrl里的值可以傳多個值

當然了,為了方便,我寫為一個類,可以根據自己的需要,進行調整,滿足自己的需求即可。

源碼


<?php

/**
 * 通過新浪微博API,生成短鏈接,支持一次性轉多個長鏈接
 * Class shortClass
 * @time 2018-08-14
 * @author gxcuizy
 */
Class ShortLink {
    // APPkey,我在網上找的(https://fengmk2.com/blog/appkey.html),可以自己申請
    protected $appKey = '569452181';
    // 轉短連接API地址
    protected $shortUrl = 'https://api.weibo.com/2/short_url/shorten.json?';

    /**
     * 生成短鏈接
     * @param array $longUrl 長鏈接數組
     * @return array 返回短連接數據
     */
    public function getShortUrl($longUrl = []) {
        $code = true;
        $msg = '請求成功!';
        $result = [];
        // 長鏈接數組為空,不處理
        if (empty($longUrl)) {
            $code = false;
            $msg = '長鏈接數據不能為空';
            return ['code' => $code, 'msg' => $msg, 'result' => $result];
        }
        // 拼接請求URL
        $longUrlStr = $this->_getLongUrl($longUrl);
        $shortUrl = $this->shortUrl;
        $appKey = $this->appKey;
        $param = 'source=' . $appKey . '&' . $longUrlStr;
        $curlUrl = $shortUrl . $param;
        // 發送CURL請求
        $result = $this->_sendCurl($curlUrl);
        return ['code' => $code, 'msg' => $msg, 'result' => $result];
    }

    /**
     * 獲取請求URL字符串
     * @param array $longUrl 長鏈接數組
     * @return string 長鏈接URL字符串
     */
    private function _getLongUrl($longUrl = []) {
        $str = '';
        foreach ($longUrl as $url) {
            $str .= ('url_long=' . $url . '&');
        }
        $newStr = substr($str, 0, strlen($str) - 1);
        return $newStr;
    }

    /**
     * 發送CURL請求(GET)
     * @param string $curlUrl 請求地址
     * @return array 返回信息
     */
    private function _sendCurl($curlUrl) {
        // 初始化
        $ch = curl_init();
        // 設置選項,包括URL
        curl_setopt($ch, CURLOPT_URL, $curlUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        // 執行並獲取HTML文檔內容
        $output = curl_exec($ch);
        // 釋放curl句柄
        curl_close($ch);
        // Json數據轉為數組
        $result = json_decode($output, true);
        return $result;
    }
}

// 實例化對象
$shortObj = new ShortLink();
// 多個連接可以直接放到數組中,類似$longUrl = ['url1', 'url2', ……]
$longUrl = ['http://blog.y0701.com/index.html'];
// 開始轉長鏈接為短鏈接
$result = $shortObj->getShortUrl($longUrl);
print_r($result);

結束語

上面說到的網上查找得到的一些AppKey,因為來源不明,所以,不建議用於生產環境,需要用於生產環境的話,建議直接在新浪微博開發者平台里創建自己的應用就行。

原文地址:https://segmentfault.com/a/1190000016004175


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM