bit.ly API の PHP class version 0.0.3

version 0.0.3にアップデート
メソッドの追加とconstructのパラメータ変更


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

  • 追記 2009/09/22

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

<?php
/**
 * Bitly class
 *
 * Changelog since 0.0.1
 * - change errors method
 * - added errorMessage method
 *
 * Changelog since 0.0.2
 * - change construct method parameters
 * - added info method
 * - added stats method
 *
 *
 * @author      tknzk <info@tknzk.com>
 * @version     0.0.3
 * @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.3';


    /**
     * 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, $apikey, $apiversion = '2.0.1', $format = 'json')
    {
        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);

        $curl = curl_init();
        curl_setopt($curl,  CURLOPT_URL,            $apiurl);
        curl_setopt($curl,  CURLOPT_HEADER,         false);
        curl_setopt($curl,  CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);
        curl_close($curl);

        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'];
            }else{
                return false;
            }
        }
        if($this->format === 'xml') {

            $xml = simplexml_load_string($response);

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

    /**
     * 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);

        $curl = curl_init();
        curl_setopt($curl,  CURLOPT_URL,            $apiurl);
        curl_setopt($curl,  CURLOPT_HEADER,         false);
        curl_setopt($curl,  CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);
        curl_close($curl);

        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'];
            }else{
                return false;
            }
        }

        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;
            }else{
                return false;
            }
        }
    }

    /**
     * Get bit.ly API error code Message
     *
     * @access      public
     * @param       string  $errorcode
     * @return      string
     * @static
     */
    public function errorMessge($errorcode)
    {
        $apiurl = self::BITLY_API_URL   . '/errors?'
                                        . 'version='    . $this->apiversion
                                        . '&login='     . $this->login
                                        . '&apiKey='    . $this->apikey
                                        . '&format='    . $this->format
                                        . '';

        //$response = file_get_contents($apiurl);

        $curl = curl_init();
        curl_setopt($curl,  CURLOPT_URL,            $apiurl);
        curl_setopt($curl,  CURLOPT_HEADER,         false);
        curl_setopt($curl,  CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);
        curl_close($curl);

        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;
        //    }
        //}
    }

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

        //$response = file_get_contents($apiurl);

        $curl = curl_init();
        curl_setopt($curl,  CURLOPT_URL,            $apiurl);
        curl_setopt($curl,  CURLOPT_HEADER,         false);
        curl_setopt($curl,  CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);
        curl_close($curl);

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

            $json = json_decode($response,true);
            return $json;

        }

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

        //    $xml = simplexml_load_string($response);

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

    /**
     * Get a list of bit.ly API information
     *
     * @access      public
     * @param       string  $url
     * @return      string
     * @static
     */
    public function info($url)
    {
        $apiurl = self::BITLY_API_URL   . '/info?'
                                        . 'version='    . $this->apiversion
                                        . '&shortUrl='  . urlencode($url)
                                        . '&login='     . $this->login
                                        . '&apiKey='    . $this->apikey
                                        . '&format='    . $this->format
                                        . '';

        $curl = curl_init();
        curl_setopt($curl,  CURLOPT_URL,            $apiurl);
        curl_setopt($curl,  CURLOPT_HEADER,         false);
        curl_setopt($curl,  CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);
        curl_close($curl);

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

            $json = json_decode($response,true);
            return $json;

        }

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

        //    $xml = simplexml_load_string($response);

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

    /**
     * Get a list of bit.ly API stats
     *
     * @access      public
     * @param       string  $url
     * @return      string
     * @static
     */
    public function stats($url)
    {
        $apiurl = self::BITLY_API_URL   . '/stats?'
                                        . 'version='    . $this->apiversion
                                        . '&shortUrl='  . urlencode($url)
                                        . '&login='     . $this->login
                                        . '&apiKey='    . $this->apikey
                                        . '&format='    . $this->format
                                        . '';

        //$response = file_get_contents($apiurl);

        $curl = curl_init();
        curl_setopt($curl,  CURLOPT_URL,            $apiurl);
        curl_setopt($curl,  CURLOPT_HEADER,         false);
        curl_setopt($curl,  CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);
        curl_close($curl);

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

            $json = json_decode($response,true);
            return $json;

        }

        //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;
    }

}