PHP bit.ly类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP bit.ly类相关的知识,希望对你有一定的参考价值。

/*************************
 * bit.ly Class Copyright (c)- 2010 - Ákos Nikházy.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the 
 * GNU General Public License (GPL) Version 2, June 1991, 
 * which accompanies this distribution, and is available at: 
 * http://www.opensource.org/licenses/gpl-license.php
 
 *It creates a short URL form given URL using bit.ly API. To use it you have to register at bit.ly and then get the API KEY at http://bit.ly/a/your_api_key
 *Version 1.0
**************************/
class bitlyClass
{
    protected $bitlyLogin     = "YOUR BIT.LY LOGIN";
    protected $bitlyApikey     = "YOUR BIT.LY API KEY";
    
    public function __construct($_bitlyLogin = '',$_bitlyApikey = '')
    {
        if($_bitlyLogin != '' && $_bitlyApikey != '')
        {
            $this->bitlyLogin     = $_bitlyLogin;
            $this->bitlyApikey     = $_bitlyApikey;
        }
        
    }
    
    //The shortener function. The url paramater is needed.
    public function getShortURL($url)
    {
        $apiURL     = 'http://api.bit.ly/shorten?version=2.0.1&longUrl='.$url.'&login='.$this->bitlyLogin.'&apiKey='.$this->bitlyApikey;
        $APICall     = file_get_contents($apiURL);
        $bitlyInfo    = json_decode(utf8_encode($APICall),true);
        return ($bitlyInfo['errorCode']==0)?$bitlyInfo['results'][urldecode($url)]['shortUrl']:false;
    }

} 

/* USAGE */

require_once 'bit.ly.class.php';

/*USE THIS METHOD IF API KEY PROVIDED INSIDE THE CLASS*/
//$test = new bitlyClass();
//echo $test->getShortURL('http://www.google.com');

$test = new bitlyClass('YOUR BIT.LY LOGIN','YOUR BIT.LY API KEY');
echo 'http://www.google.com shortened version: '.$test->getShortURL('http://www.google.com');

以上是关于PHP bit.ly类的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 用于将URL与您的bit.ly帐户相关联的TextExpander的bit.ly URL缩短脚本

Ruby on Rails - 生成 bit.ly 样式标识符

集成到 Bit.ly API 版本 4 - 如何生成 OAuth2 令牌以与 Bit.ly API V4 集成并创建缩短的 url?

Angular2 bit.ly 'Access-Control-Allow-Origin' 标头问题

JavaScript bit.ly透明叠加JavaScript书签

DBMySQLSQLitePHPRubyRailsJS等的数据类型,因为我有时会忘记它们