Publisher.php 3.2 KB

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