MetamarkNet.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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-2015 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. * Metamark.net API implementation
  27. *
  28. * @category Zend
  29. * @package Zend_Service_ShortUrl
  30. * @copyright Copyright (c) 2005-2015 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_MetamarkNet extends Zend_Service_ShortUrl_AbstractShortener
  34. {
  35. /**
  36. * Base URI of the service
  37. *
  38. * @var string
  39. */
  40. protected $_baseUri = 'http://xrl.us/';
  41. protected $_apiUri = 'http://metamark.net/api/rest/simple';
  42. /**
  43. * This function shortens long url
  44. *
  45. * @param string $url URL to Shorten
  46. * @throws Zend_Service_ShortUrl_Exception When URL is not valid
  47. * @return string New URL
  48. */
  49. public function shorten($url)
  50. {
  51. $this->_validateUri($url);
  52. $this->getHttpClient()->setUri($this->_apiUri);
  53. $this->getHttpClient()->setParameterGet('long_url', $url);
  54. $response = $this->getHttpClient()->request();
  55. return $response->getBody();
  56. }
  57. /**
  58. * Reveals target for short URL
  59. *
  60. * @param string $shortenedUrl URL to reveal target of
  61. * @throws Zend_Service_ShortUrl_Exception When URL is not valid or is not shortened by this service
  62. * @return string
  63. */
  64. public function unshorten($shortenedUrl)
  65. {
  66. $this->_validateUri($shortenedUrl);
  67. $this->_verifyBaseUri($shortenedUrl);
  68. $this->getHttpClient()->setUri($this->_apiUri);
  69. $this->getHttpClient()->setParameterGet('short_url', $shortenedUrl);
  70. $response = $this->getHttpClient()->request();
  71. return $response->getBody();
  72. }
  73. }