11<?php
22/**
33 * A PHP client library for pubsubhubbub
4- *
4+ *
55 * @link http://code.google.com/p/pubsubhubbub/
66 * @author Josh Fraser | joshfraser.com | josh@eventvue.com
77 * @license Apache License 2.0
88 */
99
10- namespace pubsubhubbub /subscriber ;
10+ namespace Pubsubhubbub \ Subscriber ;
1111
1212class Subscriber {
1313 /**
1414 * put your google key here
1515 * required if you want to use the google feed API to lookup RSS feeds
16- *
16+ *
1717 * @var string
1818 */
1919 protected $ google_key = "" ;
20-
20+
2121 /** @var string */
2222 protected $ hub_url ;
23-
23+
2424 /** @var string */
2525 protected $ callback_url ;
26-
26+
2727 /** @var string */
2828 protected $ credentials ;
29-
29+
3030 /**
3131 * @var string accepted values are "async" and "sync"
3232 */
33- protected $ verify = "async " ;
34-
33+ protected $ verify = "async " ;
34+
3535 /** @var string */
3636 protected $ verify_token ;
37-
37+
3838 /** @var int */
3939 protected $ lease_seconds ;
40-
40+
4141 /**
4242 * create a new Subscriber (credentials added for SuperFeedr support)
43- *
43+ *
4444 * @param string $hub_url
4545 * @param string $callback_url
4646 * @param string $credentials
4747 */
4848 public function __construct ($ hub_url , $ callback_url , $ credentials = false ) {
49-
49+
5050 if (!isset ($ hub_url ))
5151 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 ))
5454 throw new Exception ('The specified hub url does not appear to be valid: ' .$ hub_url );
55-
55+
5656 if (!isset ($ callback_url ))
5757 throw new Exception ('Please specify a callback ' );
58-
58+
5959 $ this ->hub_url = $ hub_url ;
6060 $ this ->callback_url = $ callback_url ;
6161 $ this ->credentials = $ credentials ;
6262 }
63-
63+
6464 /**
6565 * $use_regexp lets you choose whether to use google AJAX feed api (faster, but cached) or a regexp to read from site
66- *
66+ *
6767 * @param string $url
6868 * @param callable $http_function
6969 * @return string
7070 */
7171 public function find_feed ($ url , $ http_function = false ) {
7272 // using google feed API
7373 $ 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
7575 if ($ http_function )
7676 $ response = $ http_function ($ url );
7777 else
@@ -81,7 +81,7 @@ public function find_feed($url, $http_function = false) {
8181 $ rss_url = $ result ['responseData ' ]['url ' ];
8282 return $ rss_url ;
8383 }
84-
84+
8585 /**
8686 * @param string $topic_url
8787 * @param callable $http_function
@@ -90,7 +90,7 @@ public function find_feed($url, $http_function = false) {
9090 public function subscribe ($ topic_url , $ http_function = false ) {
9191 return $ this ->change_subscription ("subscribe " , $ topic_url , $ http_function = false );
9292 }
93-
93+
9494 /**
9595 * @param string $topic_url
9696 * @param callable $http_function
@@ -102,7 +102,7 @@ public function unsubscribe($topic_url, $http_function = false) {
102102
103103 /**
104104 * helper function since sub/unsub are handled the same way
105- *
105+ *
106106 * @param string $mode
107107 * @param string $topic_url
108108 * @param callable $http_function
@@ -111,61 +111,61 @@ public function unsubscribe($topic_url, $http_function = false) {
111111 private function change_subscription ($ mode , $ topic_url , $ http_function = false ) {
112112 if (!isset ($ topic_url ))
113113 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 ))
117117 throw new Exception ('The specified topic url does not appear to be valid: ' .$ topic_url );
118-
118+
119119 // set the mode subscribe/unsubscribe
120120 $ post_string = "hub.mode= " .$ mode ;
121121 $ post_string .= "&hub.callback= " .urlencode ($ this ->callback_url );
122122 $ post_string .= "&hub.verify= " .$ this ->verify ;
123123 $ post_string .= "&hub.verify_token= " .$ this ->verify_token ;
124124 $ post_string .= "&hub.lease_seconds= " .$ this ->lease_seconds ;
125-
125+
126126 // append the topic url parameters
127127 $ post_string .= "&hub.topic= " .urlencode ($ topic_url );
128-
128+
129129 // make the http post request and return true/false
130130 // easy to over-write to use your own http function
131131 if ($ http_function )
132132 return $ http_function ($ this ->hub_url ,$ post_string );
133133 else
134134 return $ this ->http ($ this ->hub_url ,$ post_string );
135135 }
136-
136+
137137 /**
138138 * default http function that uses curl to post to the hub endpoint
139- *
139+ *
140140 * @param string $url
141141 * @param string $post_string
142142 * @return mixed
143143 */
144144 private function http ($ url , $ post_string ) {
145-
145+
146146 // add any additional curl options here
147147 $ 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+
151151 if ($ post_string ) {
152152 $ options [CURLOPT_POST ] = true ;
153153 $ options [CURLOPT_POSTFIELDS ] = $ post_string ;
154154 }
155-
155+
156156 if ($ this ->credentials )
157157 $ options [CURLOPT_USERPWD ] = $ this ->credentials ;
158158
159- $ ch = curl_init ();
160- curl_setopt_array ($ ch , $ options );
159+ $ ch = curl_init ();
160+ curl_setopt_array ($ ch , $ options );
161161
162162 $ response = curl_exec ($ ch );
163163 $ info = curl_getinfo ($ ch );
164-
165- // all good -- anything in the 200 range
164+
165+ // all good -- anything in the 200 range
166166 if (substr ($ info ['http_code ' ],0 ,1 ) == "2 " ) {
167167 return $ response ;
168168 }
169- return false ;
169+ return false ;
170170 }
171171}
0 commit comments