]>
git.wh0rd.org - tt-rss.git/blob - lib/pubsubhubbub/Subscriber.php
5b980a111f4f861b25b06d6ad7770e171765cb36
3 * A PHP client library for pubsubhubbub.
5 * @link http://code.google.com/p/pubsubhubbub/
7 * @author Josh Fraser | joshfraser.com | josh@eventvue.com
8 * @license Apache License 2.0
10 namespace Pubsubhubbub\Subscriber
;
12 use InvalidArgumentException
;
17 * Put your google key here.
18 * Required if you want to use the google feed API to lookup RSS feeds.
22 protected $google_key = '';
32 protected $callback_url;
37 protected $credentials;
40 * @var string accepted values are "async" and "sync"
42 protected $verify = 'async';
47 protected $verify_token;
52 protected $lease_seconds;
55 * Create a new Subscriber (credentials added for SuperFeedr support).
57 * @param string $hub_url
58 * @param string $callback_url
59 * @param string $credentials
61 public function __construct($hub_url, $callback_url, $credentials = false)
63 if (! isset($hub_url)) {
64 throw new InvalidArgumentException('Please specify a hub url');
67 if (! preg_match('|^https?://|i', $hub_url)) {
68 throw new InvalidArgumentException('The specified hub url does not appear to be valid: ' . $hub_url);
71 if (! isset($callback_url)) {
72 throw new InvalidArgumentException('Please specify a callback');
75 $this->hub_url
= $hub_url;
76 $this->callback_url
= $callback_url;
77 $this->credentials
= $credentials;
81 * $use_regexp lets you choose whether to use google AJAX feed api (faster, but cached) or a regexp to read from site.
84 * @param callable $http_function
88 public function find_feed($url, $http_function = false)
90 // using google feed API
91 $url = "http://ajax.googleapis.com/ajax/services/feed/lookup?key={$this->google_key}&v=1.0&q=" . urlencode($url);
94 $response = $http_function($url);
96 $response = $this->http($url);
99 $result = json_decode($response, true);
100 $rss_url = $result['responseData']['url'];
106 * Subscribe to a topic.
108 * @param string $topic_url
109 * @param callable $http_function
113 public function subscribe($topic_url, $http_function = false)
115 return $this->change_subscription('subscribe', $topic_url, $http_function);
119 * Unsubscribe from a topic.
121 * @param string $topic_url
122 * @param callable $http_function
126 public function unsubscribe($topic_url, $http_function = false)
128 return $this->change_subscription('unsubscribe', $topic_url, $http_function);
132 * Helper function since sub/unsub are handled the same way.
134 * @param string $mode
135 * @param string $topic_url
136 * @param callable $http_function
140 private function change_subscription($mode, $topic_url, $http_function = false)
142 if (! isset($topic_url)) {
143 throw new InvalidArgumentException('Please specify a topic url');
146 // lightweight check that we're actually working w/ a valid url
147 if (! preg_match('|^https?://|i', $topic_url)) {
148 throw new InvalidArgumentException('The specified topic url does not appear to be valid: ' . $topic_url);
151 // set the mode subscribe/unsubscribe
152 $post_string = 'hub.mode=' . $mode;
153 $post_string .= '&hub.callback=' . urlencode($this->callback_url
);
154 $post_string .= '&hub.verify=' . $this->verify
;
155 $post_string .= '&hub.verify_token=' . $this->verify_token
;
156 $post_string .= '&hub.lease_seconds=' . $this->lease_seconds
;
158 // append the topic url parameters
159 $post_string .= '&hub.topic=' . urlencode($topic_url);
161 // make the http post request and return true/false
162 // easy to over-write to use your own http function
163 if ($http_function) {
164 return call_user_func_array($http_function, [$this->hub_url
, $post_string]);
167 return $this->http($this->hub_url
, $post_string);
171 * Default http function that uses curl to post to the hub endpoint.
174 * @param string $post_string
178 private function http($url, $post_string)
181 // add any additional curl options here
184 CURLOPT_USERAGENT
=> 'PubSubHubbub-Subscriber-PHP/1.0',
185 CURLOPT_RETURNTRANSFER
=> true,
189 $options[CURLOPT_POST
] = true;
190 $options[CURLOPT_POSTFIELDS
] = $post_string;
193 if ($this->credentials
) {
194 $options[CURLOPT_USERPWD
] = $this->credentials
;
198 curl_setopt_array($ch, $options);
200 $response = curl_exec($ch);
201 $info = curl_getinfo($ch);
203 // all good -- anything in the 200 range
204 if (substr($info['http_code'], 0, 1) == '2') {