O'Reilly logo

Programming Social Applications by Jonathan LeBlanc

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Building a Publisher in 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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required