/components/ShortenComponent.php

https://github.com/MadMikeyB/CakePHP-Scrapbook · PHP · 130 lines · 83 code · 19 blank · 28 comment · 23 complexity · 4dd6c283d117e44b8c11841107acbda6 MD5 · raw file

  1. <?php
  2. App::uses('HttpSocket', 'Network/Http'); //cake 2.x
  3. class ShortenComponent extends Component
  4. {
  5. var $name = 'Shorten';
  6. public $bitlyLogin; // obtain from https://bitly.com/a/your_api_key
  7. public $bitlyApiKey; // obtain from https://bitly.com/a/your_api_key
  8. public $bitlyFormat; // json, xml, txt
  9. public $kwnDomain; // kwn.me or quicklink.me
  10. public $kwnApiKey; // obtain from kwn.me/api?act=key
  11. public $yourlsDomain; // the domain YOURLS is installed on
  12. public $yourlsFormat; // json, xml, simple
  13. public $googleDomain; // Google API's URL
  14. public $googleApiKey; // obtain from https://code.google.com/apis/console/
  15. /**
  16. * ShortenComponent::shorten()
  17. *
  18. * @param mixed $longurl
  19. * @param mixed $service
  20. * @param mixed $options
  21. * @return mixed $shorturl
  22. *
  23. * @author Michael Burton (MadMikeyB)
  24. * @copyright 2011
  25. * @link http://onemorefunction.com/blog/posts/cakephp-shortencomponent-a-component-for-url-shortening/9/
  26. * @example http://onemorefunction.com/blog/examples/shorten
  27. * @license BSD - https://github.com/MadMikeyB/CakePHP-Scrapbook/blob/master/LICENSE
  28. */
  29. public function shorten($longurl, $service, $options = array())
  30. {
  31. if (!empty($longurl)) {
  32. if ($service == 'kwnme') {
  33. // define all settings
  34. if (empty($options['kwn'])) { // check if the user has passed any args with the shorten func
  35. $this->kwnApiKey = 'REPLACEME'; // get from kwn.me/api?act=key
  36. $this->kwnDomain = 'kwn.me'; // can be kwn.me or quicklink.me
  37. } else {
  38. $this->kwnApiKey = $options['kwn']['ApiKey'];
  39. $this->kwnDomain = $options['kwn']['Domain'];
  40. }
  41. // get stuff done
  42. $kwnmeshortenedURL = file_get_contents("http://{$this->kwnDomain}/api.php?act=shorten&key={$this->kwnApiKey}&opt=text&url=" .
  43. urlencode($longurl) . "&type=plain&recycle=true");
  44. if (!empty($kwnmeshortenedURL)) {
  45. return $kwnmeshortenedURL;
  46. } else {
  47. return 'error';
  48. }
  49. }
  50. /**
  51. * @example $this->Shorten->shorten('http://onemorefunction.com', 'bitly', array('bitly', array('Login' => 'REPLACEME', 'ApiKey' => 'REPLACEME', 'Format' => 'txt')));
  52. **/
  53. if ($service == 'bitly') {
  54. // define all settings
  55. if (empty($options['bitly'])) { // check if the user has passed any args with the shorten func
  56. $this->bitlyLogin = 'REPLACEME'; // https://bitly.com/a/your_api_key
  57. $this->bitlyApiKey = 'REPLACEME'; // https://bitly.com/a/your_api_key
  58. $this->bitlyFormat = 'txt'; // txt, json or xml
  59. } else { // args passed?
  60. $this->bitlyLogin = $options['bitly']['Login'];
  61. $this->bitlyApiKey = $options['bitly']['ApiKey'];
  62. $this->bitlyFormat = $options['bitly']['Format'];
  63. }
  64. // get stuff done
  65. $bitlyshortenedURL = file_get_contents("http://api.bitly.com/v3/shorten?login={$this->bitlyLogin}&apiKey={$this->bitlyApiKey}&longUrl={$longurl}&format={$this->bitlyFormat}");
  66. if (!empty($bitlyshortenedURL)) {
  67. return $bitlyshortenedURL;
  68. } else {
  69. return 'error';
  70. }
  71. }
  72. if ($service == 'yourls') {
  73. // define all settings
  74. if (empty($options['yourls'])) { // check if the user has passed any args with the shorten func
  75. $this->yourlsDomain = 'topic.to';
  76. } else {
  77. $this->yourlsDomain = $options['yourls']['Domain'];
  78. }
  79. $this->yourlsFormat = 'simple'; // json, xml or simple
  80. // get stuff done
  81. $yourlsshortenedURL = file_get_contents("http://{$this->yourlsDomain}/api.php?action=shorturl&url={$longurl}&format={$this->yourlsFormat}");
  82. if (!empty($yourlsshortenedURL)) {
  83. return $yourlsshortenedURL;
  84. } else {
  85. return 'error';
  86. }
  87. }
  88. if ($service == 'google') {
  89. // define all settings
  90. if (empty($options['google'])) { // check if the user has passed any args with the shorten func
  91. $this->googleDomain = 'https://www.googleapis.com/urlshortener/v1/url?key=';
  92. $this->googleApiKey = 'REPLACEME'; // get here: https://code.google.com/apis/console/
  93. } else {
  94. $this->googleDomain = $options['google']['Domain'];
  95. $this->googleApiKey = $options['google']['ApiKey'];
  96. }
  97. /**
  98. * @link https://github.com/fabricioferracioli/CakePHP-Google-URL-Shortener-Component/blob/master/google_url_shortener.php
  99. **/
  100. //App::import('Core', 'HttpSocket'); deleted for cake 2.x
  101. $socket = new HttpSocket();
  102. $result = $socket->post($this->googleDomain . $this->googleApiKey, json_encode(array
  103. ('longUrl' => $longurl)), array('header' => array('Content-Type' =>
  104. 'application/json')));
  105. $googleUrl = json_decode($result, true);
  106. return $googleUrl['id'];
  107. }
  108. }
  109. }
  110. }
  111. ?>