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