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