]> git.wh0rd.org - tt-rss.git/blame - lib/pubsubhubbub/Publisher.php
lib: Upgrade php-publisher from ??? to a5d6a0e (2016-11-15)
[tt-rss.git] / lib / pubsubhubbub / Publisher.php
CommitLineData
5ddc3e27
AK
1<?php
2/**
3 * a PHP client library for pubsubhubbub.
4 *
5 * @link https://github.com/pubsubhubbub/
6 *
7 * @author Josh Fraser | joshfraser.com | josh@eventvue.com
8 * @license Apache License 2.0
9 */
10namespace pubsubhubbub\publisher;
11
12use InvalidArgumentException;
13
14class Publisher
15{
16 /**
17 * @var string
18 */
19 protected $hub_url;
20
21 /**
22 * @var string
23 */
24 protected $last_response;
25
26 /**
27 * Create a new Publisher.
28 *
29 * @param string $hub_url
30 */
31 public function __construct($hub_url)
32 {
33 if (! isset($hub_url)) {
34 throw new InvalidArgumentException('Please specify a hub url');
35 }
36
37 if (! preg_match('|^https?://|i', $hub_url)) {
38 throw new InvalidArgumentException('The specified hub url does not appear to be valid: ' . $hub_url);
39 }
40
41 $this->hub_url = $hub_url;
42 }
43
44 /**
45 * Accepts either a single url or an array of urls.
46 *
47 * @param string|array $topic_urls
48 * @param callable $http_function
49 *
50 * @return mixed
51 */
52 public function publish_update($topic_urls, $http_function = false)
53 {
54 if (! isset($topic_urls)) {
55 throw new InvalidArgumentException('Please specify a topic url');
56 }
57
58 // check that we're working with an array
59 if (! is_array($topic_urls)) {
60 $topic_urls = [$topic_urls];
61 }
62
63 // set the mode to publish
64 $post_string = 'hub.mode=publish';
65 // loop through each topic url
66 foreach ($topic_urls as $topic_url) {
67
68 // lightweight check that we're actually working w/ a valid url
69 if (! preg_match('|^https?://|i', $topic_url)) {
70 throw new InvalidArgumentException('The specified topic url does not appear to be valid: ' . $topic_url);
71 }
72
73 // append the topic url parameters
74 $post_string .= '&hub.url=' . urlencode($topic_url);
75 }
76
77 // make the http post request and return true/false
78 // easy to over-write to use your own http function
79 if ($http_function) {
80 return $http_function($this->hub_url, $post_string);
81 }
82
83 return $this->http_post($this->hub_url, $post_string);
84 }
85
86 /**
87 * Returns any error message from the latest request.
88 *
89 * @return string
90 */
91 public function last_response()
92 {
93 return $this->last_response;
94 }
95
96 /**
97 * Default http function that uses curl to post to the hub endpoint.
98 *
99 * @param string $url
100 * @param string $post_string
101 *
102 * @return bool
103 */
104 private function http_post($url, $post_string)
105 {
106 // add any additional curl options here
107 $options = [
108 CURLOPT_URL => $url,
109 CURLOPT_POST => true,
110 CURLOPT_POSTFIELDS => $post_string,
111 CURLOPT_USERAGENT => 'PubSubHubbub-Publisher-PHP/1.0',
112 ];
113
114 $ch = curl_init();
115 curl_setopt_array($ch, $options);
116
117 $response = curl_exec($ch);
118 $this->last_response = $response;
119 $info = curl_getinfo($ch);
120
121 curl_close($ch);
122
123 return $info['http_code'] == 204;
124 }
125}