IsGd.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. * Is.gd 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_IsGd extends Zend_Service_ShortUrl_AbstractShortener
  34. {
  35. /**
  36. * Base URI of the service
  37. *
  38. * @var string
  39. */
  40. protected $_baseUri = 'http://is.gd';
  41. /**
  42. * This function shortens long url
  43. *
  44. * @param string $url URL to Shorten
  45. * @throws Zend_Service_ShortUrl_Exception When URL is not valid
  46. * @return string New URL
  47. */
  48. public function shorten($url)
  49. {
  50. $this->_validateUri($url);
  51. $serviceUri = 'http://is.gd/api.php';
  52. $this->getHttpClient()->resetParameters(true);
  53. $this->getHttpClient()->setUri($serviceUri);
  54. $this->getHttpClient()->setParameterGet('longurl', $url);
  55. $response = $this->getHttpClient()->request();
  56. return $response->getBody();
  57. }
  58. /**
  59. * Reveals target for short URL
  60. *
  61. * @param string $shortenedUrl URL to reveal target of
  62. * @throws Zend_Service_ShortUrl_Exception When URL is not valid or is not shortened by this service
  63. * @return string
  64. */
  65. public function unshorten($shortenedUrl)
  66. {
  67. $this->_validateUri($shortenedUrl);
  68. $this->_verifyBaseUri($shortenedUrl);
  69. $this->getHttpClient()->resetParameters(true);
  70. $this->getHttpClient()->setUri($shortenedUrl);
  71. $this->getHttpClient()->setConfig(array('maxredirects' => 0));
  72. $response = $this->getHttpClient()->request();
  73. if ($response->isError()) {
  74. require_once 'Zend/Service/ShortUrl/Exception.php';
  75. throw new Zend_Service_ShortUrl_Exception($response->getMessage());
  76. }
  77. if ($response->isRedirect()) {
  78. return $response->getHeader('Location');
  79. }
  80. require_once 'Zend/Service/ShortUrl/Exception.php';
  81. throw new Zend_Service_ShortUrl_Exception('Url unshortening was not successful');
  82. }
  83. }