]>
Commit | Line | Data |
---|---|---|
b0f379df 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 Publisher { | |
9 | ||
10 | protected $hub_url; | |
11 | protected $last_response; | |
12 | ||
13 | // create a new Publisher | |
14 | public function __construct($hub_url) { | |
15 | ||
16 | if (!isset($hub_url)) | |
17 | throw new Exception('Please specify a hub url'); | |
18 | ||
19 | if (!preg_match("|^https?://|i",$hub_url)) | |
20 | throw new Exception('The specified hub url does not appear to be valid: '.$hub_url); | |
21 | ||
22 | $this->hub_url = $hub_url; | |
23 | } | |
24 | ||
25 | // accepts either a single url or an array of urls | |
26 | public function publish_update($topic_urls, $http_function = false) { | |
27 | if (!isset($topic_urls)) | |
28 | throw new Exception('Please specify a topic url'); | |
29 | ||
30 | // check that we're working with an array | |
31 | if (!is_array($topic_urls)) { | |
32 | $topic_urls = array($topic_urls); | |
33 | } | |
34 | ||
35 | // set the mode to publish | |
36 | $post_string = "hub.mode=publish"; | |
37 | // loop through each topic url | |
38 | foreach ($topic_urls as $topic_url) { | |
39 | ||
40 | // lightweight check that we're actually working w/ a valid url | |
41 | if (!preg_match("|^https?://|i",$topic_url)) | |
42 | throw new Exception('The specified topic url does not appear to be valid: '.$topic_url); | |
43 | ||
44 | // append the topic url parameters | |
45 | $post_string .= "&hub.url=".urlencode($topic_url); | |
46 | } | |
47 | ||
48 | // make the http post request and return true/false | |
49 | // easy to over-write to use your own http function | |
50 | if ($http_function) | |
51 | return $http_function($this->hub_url,$post_string); | |
52 | else | |
53 | return $this->http_post($this->hub_url,$post_string); | |
54 | } | |
55 | ||
56 | // returns any error message from the latest request | |
57 | public function last_response() { | |
58 | return $this->last_response; | |
59 | } | |
60 | ||
61 | // default http function that uses curl to post to the hub endpoint | |
62 | private function http_post($url, $post_string) { | |
63 | ||
64 | // add any additional curl options here | |
65 | $options = array(CURLOPT_URL => $url, | |
66 | CURLOPT_POST => true, | |
67 | CURLOPT_POSTFIELDS => $post_string, | |
68 | CURLOPT_USERAGENT => "PubSubHubbub-Publisher-PHP/1.0"); | |
69 | ||
70 | $ch = curl_init(); | |
71 | curl_setopt_array($ch, $options); | |
72 | ||
73 | $response = curl_exec($ch); | |
74 | $this->last_response = $response; | |
75 | $info = curl_getinfo($ch); | |
76 | ||
77 | curl_close($ch); | |
78 | ||
79 | // all good | |
80 | if ($info['http_code'] == 204) | |
81 | return true; | |
82 | return false; | |
83 | } | |
84 | } | |
85 | ||
86 | ?> |