]>
git.wh0rd.org - tt-rss.git/blob - lib/pubsubhubbub/Publisher.php
3 * a PHP client library for pubsubhubbub.
5 * @link https://github.com/pubsubhubbub/
7 * @author Josh Fraser | joshfraser.com | josh@eventvue.com
8 * @license Apache License 2.0
10 namespace pubsubhubbub\publisher
;
12 use InvalidArgumentException
;
24 protected $last_response;
27 * Create a new Publisher.
29 * @param string $hub_url
31 public function __construct($hub_url)
33 if (! isset($hub_url)) {
34 throw new InvalidArgumentException('Please specify a hub url');
37 if (! preg_match('|^https?://|i', $hub_url)) {
38 throw new InvalidArgumentException('The specified hub url does not appear to be valid: ' . $hub_url);
41 $this->hub_url
= $hub_url;
45 * Accepts either a single url or an array of urls.
47 * @param string|array $topic_urls
48 * @param callable $http_function
52 public function publish_update($topic_urls, $http_function = false)
54 if (! isset($topic_urls)) {
55 throw new InvalidArgumentException('Please specify a topic url');
58 // check that we're working with an array
59 if (! is_array($topic_urls)) {
60 $topic_urls = [$topic_urls];
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) {
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);
73 // append the topic url parameters
74 $post_string .= '&hub.url=' . urlencode($topic_url);
77 // make the http post request and return true/false
78 // easy to over-write to use your own http function
80 return $http_function($this->hub_url
, $post_string);
83 return $this->http_post($this->hub_url
, $post_string);
87 * Returns any error message from the latest request.
91 public function last_response()
93 return $this->last_response
;
97 * Default http function that uses curl to post to the hub endpoint.
100 * @param string $post_string
104 private function http_post($url, $post_string)
106 // add any additional curl options here
109 CURLOPT_POST
=> true,
110 CURLOPT_POSTFIELDS
=> $post_string,
111 CURLOPT_USERAGENT
=> 'PubSubHubbub-Publisher-PHP/1.0',
115 curl_setopt_array($ch, $options);
117 $response = curl_exec($ch);
118 $this->last_response
= $response;
119 $info = curl_getinfo($ch);
123 return $info['http_code'] == 204;