j.mp用のclassを作った

bit.lyがj.mpドメインでサービスを始めたらしいので
bit.ly用のclassを元にj.mp用のclassを作った。*1

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


http://j.mp/
http://blog.bit.ly/post/179664996/go-ahead-and-j-mp


  • 追記 2009/09/22

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

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

class Jmp
{
    const DEBUG = false;

    const JMP_API_URL = 'http://api.j.mp';

    const API_VERSION = '2.0.1';

    const FORMAT = 'json';

    const VERSION = '0.0.1';


    /**
     * j.mp API Login Accoumnt
     *
     * @var string
     */
    private $login;

    /**
     * j.mp API Key
     * 
     * @var string
     */
    private $apikey;

    /**
     * j.mp 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 = self::API_VERSION, $format = self::FORMAT)
    {
        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 j.mp Short URL
     *
     * @access      public
     * @param       string  $longurl
     * @return      string
     * @static
     */
    public function shorten($longurl)
    {
        $apiurl = self::JMP_API_URL . '/shorten?'
                                    . 'version='    . $this->apiversion
                                    . '&longUrl='   . urlencode($longurl) 
                                    . '&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);

            if($json['errorCode'] === 0 && $json['statusCode'] === 'OK') {
                return $json['results'][$longurl]['shortUrl'];
            }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 j.mp Long URL
     *
     * @access      public
     * @param       string  $shorturl
     * @return      string
     * @static
     */
    public function expand($shorturl)
    {
        $apiurl = self::JMP_API_URL . '/expand?'
                                    . 'version='    . $this->apiversion
                                    . '&shortUrl='  . urlencode($shorturl) 
                                    . '&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);
            $userhash = preg_replace("/http:\/\/j.mp\//","",$shorturl);

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

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

            $xml = simplexml_load_string($response);

            $userhash = preg_replace("/http:\/\/j.mp\//","",$shorturl);

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

    /**
     * Get j.mp API error code Message
     *
     * @access      public
     * @param       string  $errorcode
     * @return      string
     * @static
     */
    public function errorMessge($errorcode)
    {
        $apiurl = self::JMP_API_URL . '/errors?'
                                    . 'version='    . $this->apiversion
                                    . '&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);

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

            }

        }

    }

    /**
     * Get a list of j.mp API error codes
     *
     * @access      public
     * @return      string
     * @static
     */
    public function errors()
    {
        $apiurl = self::JMP_API_URL . '/errors?'
                                    . 'version='    . $this->apiversion
                                    . '&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;

        }

    }

    /**
     * Get a list of j.mp API information
     *
     * @access      public
     * @param       string  $url
     * @return      string
     * @static
     */
    public function info($url)
    {
        $apiurl = self::JMP_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;

        }

    }

    /**
     * Get a list of j.mp API stats
     *
     * @access      public
     * @param       string  $url
     * @return      string
     * @static
     */
    public function stats($url)
    {
        $apiurl = self::JMP_API_URL . '/stats?'
                                    . '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;

        }

    }
    

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

}

*1:といってもbit.lyのclassをコピーしてURLを調整しただけ