Subscriber.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * A PHP client library for pubsubhubbub.
  4. *
  5. * @link http://code.google.com/p/pubsubhubbub/
  6. *
  7. * @author Josh Fraser | joshfraser.com | josh@eventvue.com
  8. * @license Apache License 2.0
  9. */
  10. namespace Pubsubhubbub\Subscriber;
  11. use InvalidArgumentException;
  12. class Subscriber
  13. {
  14. /**
  15. * Put your google key here.
  16. * Required if you want to use the google feed API to lookup RSS feeds.
  17. *
  18. * @var string
  19. */
  20. protected $google_key = '';
  21. /**
  22. * @var string
  23. */
  24. protected $hub_url;
  25. /**
  26. * @var string
  27. */
  28. protected $callback_url;
  29. /**
  30. * @var string
  31. */
  32. protected $credentials;
  33. /**
  34. * @var string accepted values are "async" and "sync"
  35. */
  36. protected $verify = 'async';
  37. /**
  38. * @var string
  39. */
  40. protected $verify_token;
  41. /**
  42. * @var string
  43. */
  44. protected $lease_seconds;
  45. /**
  46. * Create a new Subscriber (credentials added for SuperFeedr support).
  47. *
  48. * @param string $hub_url
  49. * @param string $callback_url
  50. * @param string $credentials
  51. */
  52. public function __construct($hub_url, $callback_url, $credentials = false)
  53. {
  54. if (! isset($hub_url)) {
  55. throw new InvalidArgumentException('Please specify a hub url');
  56. }
  57. if (! preg_match('|^https?://|i', $hub_url)) {
  58. throw new InvalidArgumentException('The specified hub url does not appear to be valid: ' . $hub_url);
  59. }
  60. if (! isset($callback_url)) {
  61. throw new InvalidArgumentException('Please specify a callback');
  62. }
  63. $this->hub_url = $hub_url;
  64. $this->callback_url = $callback_url;
  65. $this->credentials = $credentials;
  66. }
  67. /**
  68. * $use_regexp lets you choose whether to use google AJAX feed api (faster, but cached) or a regexp to read from site.
  69. *
  70. * @param string $url
  71. * @param callable $http_function
  72. *
  73. * @return string
  74. */
  75. public function find_feed($url, $http_function = false)
  76. {
  77. // using google feed API
  78. $url = "http://ajax.googleapis.com/ajax/services/feed/lookup?key={$this->google_key}&v=1.0&q=" . urlencode($url);
  79. // fetch the content
  80. if ($http_function) {
  81. $response = $http_function($url);
  82. } else {
  83. $response = $this->http($url);
  84. }
  85. $result = json_decode($response, true);
  86. $rss_url = $result['responseData']['url'];
  87. return $rss_url;
  88. }
  89. /**
  90. * Subscribe to a topic.
  91. *
  92. * @param string $topic_url
  93. * @param callable $http_function
  94. *
  95. * @return mixed
  96. */
  97. public function subscribe($topic_url, $http_function = false)
  98. {
  99. return $this->change_subscription('subscribe', $topic_url, $http_function);
  100. }
  101. /**
  102. * Unsubscribe from a topic.
  103. *
  104. * @param string $topic_url
  105. * @param callable $http_function
  106. *
  107. * @return mixed
  108. */
  109. public function unsubscribe($topic_url, $http_function = false)
  110. {
  111. return $this->change_subscription('unsubscribe', $topic_url, $http_function);
  112. }
  113. /**
  114. * Helper function since sub/unsub are handled the same way.
  115. *
  116. * @param string $mode
  117. * @param string $topic_url
  118. * @param callable $http_function
  119. *
  120. * @return mixed
  121. */
  122. private function change_subscription($mode, $topic_url, $http_function = false)
  123. {
  124. if (! isset($topic_url)) {
  125. throw new InvalidArgumentException('Please specify a topic url');
  126. }
  127. // lightweight check that we're actually working w/ a valid url
  128. if (! preg_match('|^https?://|i', $topic_url)) {
  129. throw new InvalidArgumentException('The specified topic url does not appear to be valid: ' . $topic_url);
  130. }
  131. // set the mode subscribe/unsubscribe
  132. $post_string = 'hub.mode=' . $mode;
  133. $post_string .= '&hub.callback=' . urlencode($this->callback_url);
  134. $post_string .= '&hub.verify=' . $this->verify;
  135. $post_string .= '&hub.verify_token=' . $this->verify_token;
  136. $post_string .= '&hub.lease_seconds=' . $this->lease_seconds;
  137. // append the topic url parameters
  138. $post_string .= '&hub.topic=' . urlencode($topic_url);
  139. // make the http post request and return true/false
  140. // easy to over-write to use your own http function
  141. if ($http_function) {
  142. return call_user_func_array($http_function, [$this->hub_url, $post_string]);
  143. }
  144. return $this->http($this->hub_url, $post_string);
  145. }
  146. /**
  147. * Default http function that uses curl to post to the hub endpoint.
  148. *
  149. * @param string $url
  150. * @param string $post_string
  151. *
  152. * @return mixed
  153. */
  154. private function http($url, $post_string)
  155. {
  156. // add any additional curl options here
  157. $options = [
  158. CURLOPT_URL => $url,
  159. CURLOPT_USERAGENT => 'PubSubHubbub-Subscriber-PHP/1.0',
  160. CURLOPT_RETURNTRANSFER => true,
  161. ];
  162. if ($post_string) {
  163. $options[CURLOPT_POST] = true;
  164. $options[CURLOPT_POSTFIELDS] = $post_string;
  165. }
  166. if ($this->credentials) {
  167. $options[CURLOPT_USERPWD] = $this->credentials;
  168. }
  169. $ch = curl_init();
  170. curl_setopt_array($ch, $options);
  171. $response = curl_exec($ch);
  172. $info = curl_getinfo($ch);
  173. // all good -- anything in the 200 range
  174. if (substr($info['http_code'], 0, 1) == '2') {
  175. return $response;
  176. }
  177. return false;
  178. }
  179. }