TinyUrlCom.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. * TinyUrl.com 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_TinyUrlCom extends Zend_Service_ShortUrl_AbstractShortener
  34. {
  35. /**
  36. * Base URI of the service
  37. *
  38. * @var string
  39. */
  40. protected $_baseUri = 'http://tinyurl.com';
  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://tinyurl.com/api-create.php';
  52. $this->getHttpClient()->setUri($serviceUri);
  53. $this->getHttpClient()->setParameterGet('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. //TinyUrl.com does not have an API for that, but we can use preview feature
  69. //we need new Zend_Http_Client
  70. $this->setHttpClient(new Zend_Http_Client());
  71. $this->getHttpClient()
  72. ->setCookie('preview', 1)
  73. ->setUri($shortenedUrl);
  74. //get response
  75. $response = $this->getHttpClient()->request();
  76. require_once 'Zend/Dom/Query.php';
  77. $dom = new Zend_Dom_Query($response->getBody());
  78. //find the redirect url link
  79. $results = $dom->query('a#redirecturl');
  80. //get href
  81. $originalUrl = $results->current()->getAttribute('href');
  82. return $originalUrl;
  83. }
  84. }