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
11 protected $last_response;
13 // create a new Publisher
14 public function __construct($hub_url) {
17 throw new Exception('Please specify a hub url');
19 if (!preg_match("|^https?://|i",$hub_url))
20 throw new Exception('The specified hub url does not appear to be valid: '.$hub_url);
22 $this->hub_url = $hub_url;
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');
30 // check that we're working with an array
31 if (!is_array($topic_urls)) {
32 $topic_urls = array($topic_urls);
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) {
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);
44 // append the topic url parameters
45 $post_string .= "&hub.url=".urlencode($topic_url);
48 // make the http post request and return true/false
49 // easy to over-write to use your own http function
51 return $http_function($this->hub_url,$post_string);
53 return $this->http_post($this->hub_url,$post_string);
56 // returns any error message from the latest request
57 public function last_response() {
58 return $this->last_response;
61 // default http function that uses curl to post to the hub endpoint
62 private function http_post($url, $post_string) {
64 // add any additional curl options here
65 $options = array(CURLOPT_URL => $url,
67 CURLOPT_POSTFIELDS => $post_string,
68 CURLOPT_USERAGENT => "PubSubHubbub-Publisher-PHP/1.0");
71 curl_setopt_array($ch, $options);
73 $response = curl_exec($ch);
74 $this->last_response = $response;
75 $info = curl_getinfo($ch);
80 if ($info['http_code'] == 204)