subscriber.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // a PHP client library for pubsubhubbub
  3. // as defined at http://code.google.com/p/pubsubhubbub/
  4. // written by Josh Fraser | joshfraser.com | josh@eventvue.com
  5. // Released under Apache License 2.0
  6. class Subscriber {
  7. // put your google key here
  8. // required if you want to use the google feed API to lookup RSS feeds
  9. protected $google_key = "";
  10. protected $hub_url;
  11. protected $callback_url;
  12. protected $credentials;
  13. // accepted values are "async" and "sync"
  14. protected $verify = "async";
  15. protected $verify_token;
  16. protected $lease_seconds;
  17. // create a new Subscriber (credentials added for SuperFeedr support)
  18. public function __construct($hub_url, $callback_url, $credentials = false) {
  19. if (!isset($hub_url))
  20. throw new Exception('Please specify a hub url');
  21. if (!preg_match("|^https?://|i",$hub_url))
  22. throw new Exception('The specified hub url does not appear to be valid: '.$hub_url);
  23. if (!isset($callback_url))
  24. throw new Exception('Please specify a callback');
  25. $this->hub_url = $hub_url;
  26. $this->callback_url = $callback_url;
  27. $this->credentials = $credentials;
  28. }
  29. // $use_regexp lets you choose whether to use google AJAX feed api (faster, but cached) or a regexp to read from site
  30. public function find_feed($url, $http_function = false) {
  31. // using google feed API
  32. $url = "http://ajax.googleapis.com/ajax/services/feed/lookup?key={$this->google_key}&v=1.0&q=".urlencode($url);
  33. // fetch the content
  34. if ($http_function)
  35. $response = $http_function($url);
  36. else
  37. $response = $this->http($url);
  38. $result = json_decode($response, true);
  39. $rss_url = $result['responseData']['url'];
  40. return $rss_url;
  41. }
  42. public function subscribe($topic_url, $http_function = false) {
  43. return $this->change_subscription("subscribe", $topic_url, $http_function = false);
  44. }
  45. public function unsubscribe($topic_url, $http_function = false) {
  46. return $this->change_subscription("unsubscribe", $topic_url, $http_function = false);
  47. }
  48. // helper function since sub/unsub are handled the same way
  49. private function change_subscription($mode, $topic_url, $http_function = false) {
  50. if (!isset($topic_url))
  51. throw new Exception('Please specify a topic url');
  52. // lightweight check that we're actually working w/ a valid url
  53. if (!preg_match("|^https?://|i",$topic_url))
  54. throw new Exception('The specified topic url does not appear to be valid: '.$topic_url);
  55. // set the mode subscribe/unsubscribe
  56. $post_string = "hub.mode=".$mode;
  57. $post_string .= "&hub.callback=".urlencode($this->callback_url);
  58. $post_string .= "&hub.verify=".$this->verify;
  59. $post_string .= "&hub.verify_token=".$this->verify_token;
  60. $post_string .= "&hub.lease_seconds=".$this->lease_seconds;
  61. // append the topic url parameters
  62. $post_string .= "&hub.topic=".urlencode($topic_url);
  63. // make the http post request and return true/false
  64. // easy to over-write to use your own http function
  65. if ($http_function)
  66. return $http_function($this->hub_url,$post_string);
  67. else
  68. return $this->http($this->hub_url,$post_string);
  69. }
  70. // default http function that uses curl to post to the hub endpoint
  71. private function http($url, $post_string) {
  72. // add any additional curl options here
  73. $options = array(CURLOPT_URL => $url,
  74. CURLOPT_USERAGENT => "PubSubHubbub-Subscriber-PHP/1.0",
  75. CURLOPT_RETURNTRANSFER => true);
  76. if ($post_string) {
  77. $options[CURLOPT_POST] = true;
  78. $options[CURLOPT_POSTFIELDS] = $post_string;
  79. }
  80. if ($this->credentials)
  81. $options[CURLOPT_USERPWD] = $this->credentials;
  82. $ch = curl_init();
  83. curl_setopt_array($ch, $options);
  84. $response = curl_exec($ch);
  85. $info = curl_getinfo($ch);
  86. // all good -- anything in the 200 range
  87. if (substr($info['http_code'],0,1) == "2") {
  88. return $response;
  89. }
  90. return false;
  91. }
  92. }
  93. ?>