BitLy.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_ShortUrl
  17. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Service_ShortUrl_AbstractShortener
  23. */
  24. require_once 'Zend/Service/ShortUrl/AbstractShortener.php';
  25. /**
  26. * Bit.ly API implementation
  27. *
  28. * @category Zend
  29. * @package Zend_Service_ShortUrl
  30. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Service_ShortUrl_BitLy extends Zend_Service_ShortUrl_AbstractShortener
  34. {
  35. /**
  36. * Base URI of the service
  37. *
  38. * @var string
  39. */
  40. protected $_apiUri = 'http://api.bitly.com';
  41. /**
  42. * user login name
  43. *
  44. * @var string
  45. */
  46. protected $_loginName;
  47. /**
  48. * user API key or application access token
  49. *
  50. * @var string
  51. */
  52. protected $_apiKey;
  53. /**
  54. * @param string $login user login name or application access token
  55. * @param null|string $apiKey user API key
  56. */
  57. public function __construct($login, $apiKey = null)
  58. {
  59. if(null === $apiKey) {
  60. $this->setOAuthAccessToken($login);
  61. } else {
  62. $this->setApiLogin($login, $apiKey);
  63. }
  64. }
  65. /**
  66. * set OAuth credentials
  67. *
  68. * @param $accessToken
  69. * @return Zend_Service_ShortUrl_BitLy
  70. */
  71. public function setOAuthAccessToken($accessToken)
  72. {
  73. $this->_apiKey = $accessToken;
  74. $this->_loginName = null;
  75. return $this;
  76. }
  77. /**
  78. * set login credentials
  79. *
  80. * @param $login
  81. * @param $apiKey
  82. * @return Zend_Service_ShortUrl_BitLy
  83. */
  84. public function setApiLogin($login, $apiKey)
  85. {
  86. $this->_apiKey = $apiKey;
  87. $this->_loginName = $login;
  88. return $this;
  89. }
  90. /**
  91. * prepare http client
  92. * @return void
  93. */
  94. protected function _setAccessParameter()
  95. {
  96. if(null === $this->_loginName) {
  97. //OAuth login
  98. $this->getHttpClient()->setParameterGet('access_token', $this->_apiKey);
  99. } else {
  100. //login/APIKey authentication
  101. $this->getHttpClient()->setParameterGet('login',$this->_loginName);
  102. $this->getHttpClient()->setParameterGet('apiKey',$this->_apiKey);
  103. }
  104. }
  105. /**
  106. * handle bit.ly response
  107. *
  108. * @return string
  109. * @throws Zend_Service_ShortUrl_Exception
  110. */
  111. protected function _processRequest()
  112. {
  113. $response = $this->getHttpClient()->request();
  114. if(500 == $response->getStatus()) {
  115. throw new Zend_Service_ShortUrl_Exception('Bit.ly :: '.$response->getBody());
  116. }
  117. return $response->getBody();
  118. }
  119. /**
  120. * This function shortens long url
  121. *
  122. * @param string $url URL to Shorten
  123. * @throws Zend_Service_ShortUrl_Exception if bit.ly reports an error
  124. * @return string Shortened Url
  125. */
  126. public function shorten($url)
  127. {
  128. $this->_validateUri($url);
  129. $this->_setAccessParameter();
  130. $this->getHttpClient()->setUri($this->_apiUri.'/v3/shorten');
  131. $this->getHttpClient()->setParameterGet('longUrl',$url);
  132. $this->getHttpClient()->setParameterGet('format','txt');
  133. return $this->_processRequest();
  134. }
  135. /**
  136. * Reveals target for short URL
  137. *
  138. * @param string $shortenedUrl URL to reveal target of
  139. * @throws Zend_Service_ShortUrl_Exception if bit.ly reports an error
  140. * @return string Unshortened Url
  141. */
  142. public function unshorten($shortenedUrl)
  143. {
  144. $this->_validateUri($shortenedUrl);
  145. $this->_setAccessParameter();
  146. $this->getHttpClient()->setUri($this->_apiUri.'/v3/expand');
  147. $this->getHttpClient()->setParameterGet('shortUrl',$shortenedUrl);
  148. $this->getHttpClient()->setParameterGet('format','txt');
  149. return $this->_processRequest();
  150. }
  151. }