The full code for this sample is available at https://github.com/jcleblanc/programming-social-applications/tree/master/chapter_10/pubsubhubbub-publisher-php.
For a closer look at this specification, let’s explore how to implement a publisher in the PubSubHubbub workflow using PHP.
We’ll start with the Publisher
class, which will provide us with all of the feed publishing
functionality that we will need for this process. In this example, this
file is stored as publisher.php:
<?php /* * Class: PubSubHubbub Publisher * Description: Allows for the publishing of new updates to the hub */ class Publisher{ private $regex_url = '|^https?://|i'; //simple URL string validator private $hub = ''; //hub URL //constructor that stores the hub and callback URLs for the subscriber public function __construct($hub){ if (preg_match($this->regex_url, $hub)){ $this->hub = $hub; } else{ throw new Exception('Invalid hub URL supplied'); } } //makes request to hub to subscribe / unsubscribe public function publish($feeds){ //set up POST string with mode $post_string = 'hub.mode=publish'; //loop through each feed provided foreach ($feeds as $feed){ //if feed is valid, add to POST string if (preg_match($this->regex_url, $feed)){ $post_string .= '&hub.url=' . urlencode($feed); } else { throw new Exception('Invalid hub URL supplied'); } } //set up cURL request $ch = curl_init($this->hub); $options = array( CURLOPT_HEADER => true, CURLINFO_HEADER_OUT => true, CURLOPT_VERBOSE => true, ...
No credit card required