bit.ly API の PHP classをつくった

短縮URLサービスのbit.lyのAPIをたたくPHP classをつくった
classのメソッドなどはhttp://classes.verkoyen.eu/twitter/PEAR Services_TinyURLを参考に作成

http://tknzk.com/lab/bitly/
http://tknzk.com/lab/bitly/bitly.phps


http://bit.ly/
http://bit.ly/pages/tools/developer-tools/
http://code.google.com/p/bitly-api/wiki/ApiDocumentation

  • 追記 2009/09/22

Openpear.orgにてServices_Bitlyとして改修した物を公開しました。
Services_Bitly \ Package \ Openpear



ソースは以下の通り

<?php
/**
 * Bitly class
 *
 * @author      tknzk <info@tknzk.com>
 * @version     0.0.1
 * @copyright   Copyright (c) 2009, tknzk.com All rights reserved.
 * @license     BSD License
 *
 */

class Bitly
{
    const DEBUG = false;

    const BITLY_API_URL = 'http://api.bit.ly';

    const VERSION = '0.0.1';


    /**
     * bit.ly API Login Accoumnt
     *
     * @var string
     */
    private $login;

    /**
     * bit.ly API Key
     * 
     * @var string
     */
    private $apikey;

    /**
     * bit.ly API Version
     *
     * @var string
     */
    private $apiversion;

    /**
     * Default constructor
     *
     * @return  void
     * @param   string  $login
     * @param   string  $apikey
     * @param   string  $apiversion
     * @param   string  $format
     *
     */
    public function __construct($login = null, $apikey = null, $apiversion = null, $format = null)
    {
        if($login !== null) {
            $this->setLogin($login);
        }

        if($apikey !== null) {
            $this->setApikey($apikey);
        }

        if($apiversion !== null) {
            $this->setApiVersion($apiversion);
        }

        if($format !== null) {
            $this->setFormat($format);
        }
    }

    /**
     * Create bit.ly Short URL
     *
     * @access      public
     * @param       string  $longurl
     * @return      string
     * @static
     */
    public function shorten($longurl)
    {
        $apiurl = self::BITLY_API_URL   . '/shorten?'
                                        . 'version='    . $this->apiversion
                                        . '&longUrl='   . urlencode($longurl) 
                                        . '&login='     . $this->login
                                        . '&apiKey='    . $this->apikey
                                        . '&format='    . $this->format
                                        . '';

        $response = file_get_contents($apiurl);

        if($this->format === 'json') {

            $json = json_decode($response,true);

            if($json['errorCode'] === 0 && $json['statusCode'] === 'OK') {
                return $json['results'][$longurl]['shortUrl'];
            }else{
                return $json['errorCode'];
            }
        }
        if($this->format === 'xml') {

            $xml = simplexml_load_string($response);

            if($xml->errorCode == 0 && $xml->statusCode == 'OK') {
                return $xml->results->nodeKeyVal->shortUrl;
            }
        }
     }

    /**
     * Expand bit.ly Long URL
     *
     * @access      public
     * @param       string  $shorturl
     * @return      string
     * @static
     */
    public function expand($shorturl)
    {
        $apiurl = self::BITLY_API_URL   . '/expand?'
                                        . 'version='    . $this->apiversion
                                        . '&shortUrl='  . urlencode($shorturl) 
                                        . '&login='     . $this->login
                                        . '&apiKey='    . $this->apikey
                                        . '&format='    . $this->format
                                        . '';
        $response = file_get_contents($apiurl);

        if($this->format === 'json') {

            $json = json_decode($response,true);
            $userhash = preg_replace("/http:\/\/bit.ly\//","",$shorturl);

            if($json['errorCode'] === 0 && $json['statusCode'] === 'OK') {
                return $json['results'][$userhash]['longUrl'];
            }else{
                return $json['errorCode'];
            }
        }

        if($this->format === 'xml') {

            $xml = simplexml_load_string($response);

            $userhash = preg_replace("/http:\/\/bit.ly\//","",$shorturl);

            if($xml->errorCode == 0 && $xml->statusCode == 'OK') {
                return $xml->results->$userhash->longUrl;
            }
        }
    }

    /**
     * Get a list of bit.ly API error codes
     *
     * @access      public
     * @return      string
     * @static
     */
    public function errors($errorcode)
    {
        $apiurl = self::BITLY_API_URL   . '/errors?'
                                        . 'version='    . $this->apiversion
                                        . '&login='     . $this->login
                                        . '&apiKey='    . $this->apikey
                                        . '&format='    . $this->format
                                        . '';
        $response = file_get_contents($apiurl);

        if($this->format === 'json') {

            $json = json_decode($response,true);

            foreach($json['results'] AS $results) {
                
                if($results['errorCode'] === $errorcode) {
                    return $results['errorMessage'];
                }

            }

        }

        //if($this->format === 'xml') {

        //    $xml = simplexml_load_string($response);

        //    if($xml->errorCode === 0 && $xml->statusCode === 'OK') {
        //        return $xml->results->nodeKeyVal->shortUrl;
        //    }
        //}
    }

    /**
     * Set login
     *
     * @return  void
     * @param   string
     */
    public function setLogin($login)
    {
        $this->login = (string) $login;
    }

    /**
     * Set apikey
     *
     * @return  void
     * @param   string
     */
    public function setApikey($apikey)
    {
        $this->apikey = (string) $apikey;
    }

    /**
     * Set apiversion
     *
     * @return  void
     * @param   string
     */
    public function setApiVersion($apiversion)
    {
        $this->apiversion = (string) $apiversion;
    }

    /**
     * Set format
     *
     * @return  void
     * @param   string
     */
    public function setFormat($format)
    {
        $this->format = (string) $format;
    }

}

使い方はこんな感じ

<?php
require_once 'bitly.php';

$login      = "bit.lyのアカウント";
$apikey     = "bit.lyのAPI Key";
$apiversion = "2.0.1";
$format     = "json";


$bitly = new Bitly($login,$apikey,$apiversion,$format);

$aaa = $bitly->shorten("http://tknzk.com");
echo "<br>";
echo "aaa : " . $aaa;
echo "<br>";

$bitly = new Bitly($login,$apikey,$apiversion,$format);

$bbb = $bitly->expand($aaa);

echo "<br>";
echo "bbb : " . $bbb;
echo "<br>";


$bitly = new Bitly($login,$apikey,$apiversion,$format);
$aaa = $bitly->shorten("");
$ccc = $bitly->errors($aaa);
echo "<br>";
echo "ccc : " . $ccc;
echo "<br>";

echo "<br>";