summaryrefslogtreecommitdiff
path: root/plugins/PubSubHubBub/publisher.php
blob: f176a9b8a4cb552feb52296168c3270da3f91ed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

// a PHP client library for pubsubhubbub
// as defined at http://code.google.com/p/pubsubhubbub/
// written by Josh Fraser | joshfraser.com | josh@eventvue.com
// Released under Apache License 2.0

class Publisher {
    
    protected $hub_url;
    protected $last_response;
    
    // create a new Publisher
    public function __construct($hub_url) {
        
        if (!isset($hub_url))
            throw new Exception('Please specify a hub url');
        
        if (!preg_match("|^https?://|i",$hub_url)) 
            throw new Exception('The specified hub url does not appear to be valid: '.$hub_url);
            
        $this->hub_url = $hub_url;
    }

    // accepts either a single url or an array of urls
    public function publish_update($topic_urls, $http_function = false) {
        if (!isset($topic_urls))
            throw new Exception('Please specify a topic url');
        
        // check that we're working with an array
        if (!is_array($topic_urls)) {
            $topic_urls = array($topic_urls);
        }
        
        // set the mode to publish
        $post_string = "hub.mode=publish";
        // loop through each topic url 
        foreach ($topic_urls as $topic_url) {

            // lightweight check that we're actually working w/ a valid url
            if (!preg_match("|^https?://|i",$topic_url)) 
                throw new Exception('The specified topic url does not appear to be valid: '.$topic_url);
            
            // append the topic url parameters
            $post_string .= "&hub.url=".urlencode($topic_url);
        }
        
        // make the http post request and return true/false
        // easy to over-write to use your own http function
        if ($http_function)
            return $http_function($this->hub_url,$post_string);
        else
            return $this->http_post($this->hub_url,$post_string);
    }

    // returns any error message from the latest request
    public function last_response() {
        return $this->last_response;
    }
    
    // default http function that uses curl to post to the hub endpoint
    private function http_post($url, $post_string) {
        
        // add any additional curl options here
        $options = array(CURLOPT_URL => $url,
                         CURLOPT_POST => true,
                         CURLOPT_POSTFIELDS => $post_string,
                         CURLOPT_USERAGENT => "PubSubHubbub-Publisher-PHP/1.0");

    	$ch = curl_init();
    	curl_setopt_array($ch, $options);

        $response = curl_exec($ch);
        $this->last_response = $response;
        $info = curl_getinfo($ch);

        curl_close($ch);
        
        // all good
        if ($info['http_code'] == 204) 
            return true;
        return false;	
    }
}

?>