Subversion Repositories social

[/] [stable/] [extension/] [social/] [classes/] [axsocialbitlyshortlink.php] - Rev 1

Compare with Previous | Blame | View Log

<?php

class axSocialBitlyShortLink
{
    public function __construct( $inputURI )
    {
        $ini = eZINI::instance( 'axsocial.ini' );
        $this->username = $ini->variable( 'axSocialBitlyShortLink', 'Username' );
        $this->apikey = $ini->variable( 'axSocialBitlyShortLink', 'APIKey' );
        $this->enabled = $ini->variable( 'axSocialBitlyShortLink', 'AppendShortURL' )=='enabled';
        $this->inputURI = $ini->variable( 'axSocialBitlyShortLink', 'FrontendServerURL' ) . $inputURI;
        if( !$this->username || !$this->username )
        {
            $this->enabled = false;
        }
        $this->create();
    }
   
    public function create()
    {
        if( !$this->enabled )
        {
            return false;
        }
       
        $url = 'http://api.bit.ly/shorten?version='.$this->version.'&longUrl='.urlencode($this->inputURI).'&login='.$this->username.'&apiKey='.$this->apikey.'&format='.$this->format;
        $response = file_get_contents($url);
        //parse depending on desired format
        if( strtolower( $this->format ) == 'json' )
        {
            $json = @json_decode( $response, true );
            $this->outputURI = $json['results'][$this->inputURI]['shortUrl'];
        }
        else //xml
        {
            $xml = simplexml_load_string($response);
            $this->outputURI = 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
        }
        return $this->outputURI;
    }
   
    public function length()
    {
        return strlen( $this->outputURI );
    }
   
    public $inputURI;
    public $outputURI;
   
    private $enabled;
    private $username;
    private $apikey;
    private $version = '2.0.1';
    private $format = 'json';
}

?>
 

Compare with Previous | Blame | View Log