Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes

Robo47_Service_Bitly Class Reference

List of all members.

Public Member Functions

 __construct ($login, $apiKey, $format=self::FORMAT_JSON, $resultFormat=self::FORMAT_RESULT_NATIVE, $callback= '', $version= '2.0.1')
 errors ()
 errors
 expandByHash ($hash)
 expand by hash
 expandByShortUrl ($shortUrl)
 expand by shorturl
 generateUrl ($path, array $params=array())
 Generate URL.
 getApiKey ()
 Get the api key.
 getCallback ()
 Get the callback.
 getFormat ()
 Get the format.
 getLogin ()
 Get the api login.
 getResultFormat ()
 getVersion ()
 Get Version.
 infoByHash ($hash)
 info by hash
 infoByHashWithKeys ($hash, array $keys)
 info by hash with keys
 infoByShortUrl ($shortUrl)
 info by shorturl
 infoByShortUrlWithKeys ($shortUrl, array $keys)
 info by shorturl with keys
 setApiKey ($apiKey)
 Set the api key.
 setCallback ($callback)
 Set the callback (used only if format is json).
 setFormat ($format)
 Set Format.
 setLogin ($login)
 Set the api login.
 setResultFormat ($resultFormat)
 Set Result Format.
 setVersion ($version)
 Set the version.
 shorten ($longUrl)
 shorten
 statsByHash ($hash)
 stats by Hash
 statsByShortUrl ($shortUrl)
 stats by shorturl
 xmlToArray ($xml)
 xmlToObject ($xml)

Public Attributes

const API_PATH_ERRORS = '/errors'
 API Path Errors.
const API_PATH_EXPAND = '/expand'
 API Path Expand.
const API_PATH_INFO = '/info'
 API Path Info.
const API_PATH_SHORTEN = '/shorten'
 API Path Shorten.
const API_PATH_STATS = '/stats'
 API Path Stats.
const API_URL = 'http://api.bit.ly'
 API URL.
const FORMAT_JSON = 'json'
 Format JSON.
const FORMAT_RESULT_ARRAY = 'array'
 Result Format Array.
const FORMAT_RESULT_NATIVE = 'native'
 Result Format Native.
const FORMAT_RESULT_OBJECT = 'object'
 Result Format Object.
const FORMAT_XML = 'xml'
 Format XML.

Protected Member Functions

 _callApi ($requestUri)
 Call the API.
 _getData (Zend_Http_Response $response)
 Returns data of the call if there was no error.
 _nodeToArray (DOMNode $node)
 _nodeToObject (DOMNode $node)

Protected Attributes

 $_apiKey = null
 $_callback = ''
 $_format = Robo47_Service_Bitly::FORMAT_JSON
 $_login = null
 $_resultFormat = Robo47_Service_Bitly::FORMAT_RESULT_NATIVE
 $_version = null

Detailed Description

Definition at line 31 of file Bitly.php.


Constructor & Destructor Documentation

Robo47_Service_Bitly::__construct ( login,
apiKey,
format = self::FORMAT_JSON,
resultFormat = self::FORMAT_RESULT_NATIVE,
callback = '',
version = '2.0.1' 
)
Parameters:
string $login
string $apiKey
string $format
string $resultFormat
string $callback
string $version

Definition at line 139 of file Bitly.php.

References setApiKey(), setCallback(), setFormat(), setLogin(), setResultFormat(), and setVersion().

    {
        $this->setLogin($login);
        $this->setApiKey($apiKey);
        $this->setFormat($format);
        $this->setResultFormat($resultFormat);
        $this->setCallback($callback);
        $this->setVersion($version);
    }

Here is the call graph for this function:


Member Function Documentation

Robo47_Service_Bitly::_callApi ( requestUri  )  [protected]

Call the API.

Parameters:
string $requestUri
Returns:
array

Definition at line 312 of file Bitly.php.

References _getData().

    {
        $httpClient = self::getHttpClient();
        $httpClient->setUri($requestUri);
        $response = $httpClient->request(Zend_Http_Client::GET);
        return $this->_getData($response);
    }

Here is the call graph for this function:

Robo47_Service_Bitly::_getData ( Zend_Http_Response $  response  )  [protected]

Returns data of the call if there was no error.

Exceptions:
Robo47_Service_Bitly_Exception 
Parameters:
Zend_Http_Response $response
Returns:
string|array|object

Definition at line 409 of file Bitly.php.

Referenced by _callApi().

    {
        if ($response->isError()) {
            $message = 'Error on api-call: ' . $response->getMessage();
            throw new Robo47_Service_Bitly_Exception($message);
        }

        // find errors without parsing xml / json
        // @todo parse formats always and move tests into the other switch
        //       and refactor a bit, method gets big and ugly
        switch ($this->_format) {
            case self::FORMAT_XML:
                $regex = preg_quote('<errorCode>0</errorCode>', '/');
                break;
            case self::FORMAT_JSON:
                $regex = preg_quote('"errorCode": 0', '/');
                break;
        }
        if (!preg_match('/' . $regex . '/i', $response->getBody())) {
            $message = 'Error on api-call: no errorCode=0 found';
            throw new Robo47_Service_Bitly_Exception($message);
        }

        // @todo checking for errors ALWAYS!
        switch ($this->_resultFormat) {
            case self::FORMAT_RESULT_NATIVE;
                $result = $response->getBody();
                break;
            case self::FORMAT_RESULT_ARRAY:
                switch ($this->_format) {
                    case self::FORMAT_XML:
                        $result = $this->xmlToArray($response->getBody());
                        break;
                    case self::FORMAT_JSON:
                        $result = json_decode($response->getBody(), true);
                        break;
                }
                break;
            case self::FORMAT_RESULT_OBJECT:
                switch ($this->_format) {
                    case self::FORMAT_XML:
                        $result = $this->xmlToObject($response->getBody());
                        break;
                    case self::FORMAT_JSON:
                        $result = json_decode($response->getBody(), false);
                        break;
                }
                break;
        }
        return $result;
    }

Here is the caller graph for this function:

Robo47_Service_Bitly::_nodeToArray ( DOMNode $  node  )  [protected]
Parameters:
DOMNode $node
Returns:
array|string

Definition at line 340 of file Bitly.php.

Referenced by xmlToArray().

    {
        if ($node->hasChildNodes()) {
            $array = array();
            foreach ($node->childNodes as $childNode) {
                /* @var $childNode DOMNode */
                if ($childNode->nodeName == '#text' ||
                    $childNode->nodeName == '#cdata-section') {
                    $array = $this->_nodeToArray($childNode);
                } else {
                    $nodeName = $childNode->nodeName;
                    $array[$nodeName] = $this->_nodeToArray($childNode);
                }
            }
        } else {
            $array = $node->nodeValue;

        }
        return $array;
    }

Here is the caller graph for this function:

Robo47_Service_Bitly::_nodeToObject ( DOMNode $  node  )  [protected]
Parameters:
DOMNode $node
Returns:
array|string

Definition at line 381 of file Bitly.php.

Referenced by xmlToObject().

    {
        if ($node->hasChildNodes()) {
            $object = new stdClass();
            foreach ($node->childNodes as $childNode) {
                /* @var $childNode DOMNode */
                if ($childNode->nodeName == '#text' ||
                    $childNode->nodeName == '#cdata-section') {
                    $object = $this->_nodeToArray($childNode);
                } else {
                    $nodeName = $childNode->nodeName;
                    $object->{$nodeName} = $this->_nodeToObject($childNode);
                }
            }
        } else {
            $object = $node->nodeValue;

        }
        return $object;
    }

Here is the caller graph for this function:

Robo47_Service_Bitly::errors (  ) 

errors

Returns the apis possible error-codes

Returns:
mixed

Definition at line 622 of file Bitly.php.

    {
        $url = $this->generateUrl(self::API_PATH_ERRORS, array());
        return $this->_callApi($url);
    }

Robo47_Service_Bitly::expandByHash ( hash  ) 

expand by hash

Parameters:
string $hash
Returns:
mixed

Definition at line 519 of file Bitly.php.

    {
        $params = array('hash' => $hash);
        $url = $this->generateUrl(self::API_PATH_EXPAND, $params);
        return $this->_callApi($url);
    }

Robo47_Service_Bitly::expandByShortUrl ( shortUrl  ) 

expand by shorturl

Parameters:
string $shortUrl
Returns:
mixed

Definition at line 506 of file Bitly.php.

    {
        $params = array('shortUrl' => $shortUrl);
        $url = $this->generateUrl(self::API_PATH_EXPAND, $params);
        return $this->_callApi($url);
    }

Robo47_Service_Bitly::generateUrl ( path,
array $  params = array() 
)

Generate URL.

Parameters:
string $path
array $params
Returns:
string

Definition at line 468 of file Bitly.php.

References API_URL.

    {
        $defaultParams = array();
        $defaultParams['version'] = $this->_version;
        $defaultParams['login'] = $this->_login;
        $defaultParams['apiKey'] = $this->_apiKey;
        $defaultParams['format'] = $this->_format;
        if (self::FORMAT_JSON === $defaultParams['format'] &&
            !empty($this->_callback)) {
            $defaultParams['callback'] = $this->_callback;
        }
        $defaultParams = array_merge($defaultParams, $params);

        $url = Robo47_Service_Bitly::API_URL;
        $url .= $path . '?';
        $url .= http_build_query($defaultParams, null, '&');
        return $url;
    }

Robo47_Service_Bitly::getApiKey (  ) 

Get the api key.

Returns:
string

Definition at line 267 of file Bitly.php.

    {
        return $this->_apiKey;
    }

Robo47_Service_Bitly::getCallback (  ) 

Get the callback.

Returns:
string

Definition at line 191 of file Bitly.php.

    {
        return $this->_callback;
    }

Robo47_Service_Bitly::getFormat (  ) 

Get the format.

Returns:
string

Definition at line 201 of file Bitly.php.

    {
        return $this->_format;
    }

Robo47_Service_Bitly::getLogin (  ) 

Get the api login.

Returns:
string

Definition at line 257 of file Bitly.php.

    {
        return $this->_login;
    }

Robo47_Service_Bitly::getResultFormat (  ) 
Returns:
string

Definition at line 301 of file Bitly.php.

    {
        return $this->_resultFormat;
    }

Robo47_Service_Bitly::getVersion (  ) 

Get Version.

Returns:
string

Definition at line 223 of file Bitly.php.

    {
        return $this->_version;
    }

Robo47_Service_Bitly::infoByHash ( hash  ) 

info by hash

Parameters:
string $hash
Returns:
mixed

Definition at line 564 of file Bitly.php.

    {
        $params = array('hash' => $hash);
        $url = $this->generateUrl(self::API_PATH_INFO, $params);
        return $this->_callApi($url);
    }

Robo47_Service_Bitly::infoByHashWithKeys ( hash,
array $  keys 
)

info by hash with keys

Parameters:
string $hash
array $keys
Returns:
mixed

Definition at line 578 of file Bitly.php.

    {
        $params = array(
            'hash' => $hash,
            'keys' => implode(',', $keys),
        );

        $url = $this->generateUrl(self::API_PATH_INFO, $params);
        return $this->_callApi($url);
    }

Robo47_Service_Bitly::infoByShortUrl ( shortUrl  ) 

info by shorturl

Parameters:
string $shortUrl
Returns:
mixed

Definition at line 532 of file Bitly.php.

    {
        $params = array('shortUrl' => $shortUrl);

        $url = $this->generateUrl(self::API_PATH_INFO, $params);
        return $this->_callApi($url);
    }

Robo47_Service_Bitly::infoByShortUrlWithKeys ( shortUrl,
array $  keys 
)

info by shorturl with keys

Parameters:
string $shortUrl
array $keys
Returns:
mixed

Definition at line 547 of file Bitly.php.

    {
        $params = array(
            'shortUrl' => $shortUrl,
            'keys' => implode(',', $keys),
        );

        $url = $this->generateUrl(self::API_PATH_INFO, $params);
        return $this->_callApi($url);
    }

Robo47_Service_Bitly::setApiKey ( apiKey  ) 

Set the api key.

Parameters:
string $apiKey
Returns:
Robo47_Service_Bitly *Provides Fluent Interface*

Definition at line 246 of file Bitly.php.

Referenced by __construct().

    {
        $this->_apiKey = $apiKey;
        return $this;
    }

Here is the caller graph for this function:

Robo47_Service_Bitly::setCallback ( callback  ) 

Set the callback (used only if format is json).

Parameters:
string $callback
Returns:
Robo47_Service_Bitly *Provides Fluent Interface*

Definition at line 180 of file Bitly.php.

Referenced by __construct().

    {
        $this->_callback = $callback;
        return $this;
    }

Here is the caller graph for this function:

Robo47_Service_Bitly::setFormat ( format  ) 

Set Format.

Supports json and xml

Parameters:
string $format
Returns:
Robo47_Service_Bitly *Provides Fluent Interface*

Definition at line 159 of file Bitly.php.

Referenced by __construct().

    {
        $format = strtolower($format);
        switch ($format) {
            case self::FORMAT_JSON:
            case self::FORMAT_XML:
                $this->_format = $format;
                break;
            default:
                $message = 'Invalid Format: ' . $format;
                throw new Robo47_Service_Bitly_Exception($message);
        }
        return $this;
    }

Here is the caller graph for this function:

Robo47_Service_Bitly::setLogin ( login  ) 

Set the api login.

Parameters:
string $login
Returns:
Robo47_Service_Bitly *Provides Fluent Interface*

Definition at line 234 of file Bitly.php.

Referenced by __construct().

    {
        $this->_login = $login;
        return $this;
    }

Here is the caller graph for this function:

Robo47_Service_Bitly::setResultFormat ( resultFormat  ) 

Set Result Format.

Possible Formats are native, array, object native returns the response as string

Parameters:
string $resultFormat
Returns:
Robo47_Service_Bitly *Provides Fluent Interface*

Definition at line 281 of file Bitly.php.

Referenced by __construct().

    {
        $resultFormat = strtolower($resultFormat);
        switch ($resultFormat) {
            case self::FORMAT_RESULT_NATIVE:
            case self::FORMAT_RESULT_ARRAY:
            case self::FORMAT_RESULT_OBJECT:
                $this->_resultFormat = $resultFormat;
                break;
            default:
                $message = 'Invalid Result Format: ' . $resultFormat;
                throw new Robo47_Service_Bitly_Exception($message);
        }
        return $this;
    }

Here is the caller graph for this function:

Robo47_Service_Bitly::setVersion ( version  ) 

Set the version.

Parameters:
string $version
Returns:
Robo47_Service_Bitly *Provides Fluent Interface*

Definition at line 212 of file Bitly.php.

Referenced by __construct().

    {
        $this->_version = $version;
        return $this;
    }

Here is the caller graph for this function:

Robo47_Service_Bitly::shorten ( longUrl  ) 

shorten

Parameters:
string $longUrl
Returns:
mixed

Definition at line 493 of file Bitly.php.

    {
        $params = array('longUrl' => $longUrl);
        $url = $this->generateUrl(self::API_PATH_SHORTEN, $params);
        return $this->_callApi($url);
    }

Robo47_Service_Bitly::statsByHash ( hash  ) 

stats by Hash

Parameters:
string $hash
Returns:
mixed

Definition at line 608 of file Bitly.php.

    {
        $params = array('hash' => $hash);
        $url = $this->generateUrl(self::API_PATH_STATS, $params);
        return $this->_callApi($url);
    }

Robo47_Service_Bitly::statsByShortUrl ( shortUrl  ) 

stats by shorturl

Parameters:
string $shortUrl
Returns:
mixed

Definition at line 595 of file Bitly.php.

    {
        $params = array('shortUrl' => $shortUrl);
        $url = $this->generateUrl(self::API_PATH_STATS, $params);
        return $this->_callApi($url);
    }

Robo47_Service_Bitly::xmlToArray ( xml  ) 
Parameters:
string $xml
Returns:
array

Definition at line 325 of file Bitly.php.

References _nodeToArray().

    {
        $document = new DOMDocument();
        $document->loadXml($xml);
        $array = array();
        foreach ($document->childNodes as $node) {
            $array[$node->nodeName] = $this->_nodeToArray($node);
        }
        return $array;
    }

Here is the call graph for this function:

Robo47_Service_Bitly::xmlToObject ( xml  ) 
Parameters:
string $xml
Returns:
stdClass

Definition at line 366 of file Bitly.php.

References _nodeToObject().

    {
        $document = new DOMDocument();
        $document->loadXml($xml);
        $object = new StdClass();
        foreach ($document->childNodes as $node) {
            $object->{$node->nodeName} = $this->_nodeToObject($node);
        }
        return $object;
    }

Here is the call graph for this function:


Member Data Documentation

Robo47_Service_Bitly::$_apiKey = null [protected]

Definition at line 99 of file Bitly.php.

Robo47_Service_Bitly::$_callback = '' [protected]

Definition at line 119 of file Bitly.php.

Robo47_Service_Bitly::$_format = Robo47_Service_Bitly::FORMAT_JSON [protected]

Definition at line 113 of file Bitly.php.

Robo47_Service_Bitly::$_login = null [protected]

Definition at line 93 of file Bitly.php.

Robo47_Service_Bitly::$_resultFormat = Robo47_Service_Bitly::FORMAT_RESULT_NATIVE [protected]

Definition at line 128 of file Bitly.php.

Robo47_Service_Bitly::$_version = null [protected]

Definition at line 105 of file Bitly.php.

API Path Errors.

Definition at line 61 of file Bitly.php.

API Path Expand.

Definition at line 46 of file Bitly.php.

API Path Info.

Definition at line 51 of file Bitly.php.

API Path Shorten.

Definition at line 41 of file Bitly.php.

API Path Stats.

Definition at line 56 of file Bitly.php.

const Robo47_Service_Bitly::API_URL = 'http://api.bit.ly'

API URL.

Definition at line 36 of file Bitly.php.

Referenced by generateUrl().

Format JSON.

Definition at line 66 of file Bitly.php.

Result Format Array.

Definition at line 81 of file Bitly.php.

Result Format Native.

Definition at line 86 of file Bitly.php.

Result Format Object.

Definition at line 76 of file Bitly.php.

Format XML.

Definition at line 71 of file Bitly.php.


The documentation for this class was generated from the following file: