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