The full code for this sample is available at https://github.com/jcleblanc/programming-social-applications/tree/master/chapter_10/pubsubhubbub-subscriber-php.
At this point, we’ve already explored how to set up publishers in both PHP and Python, so let’s change gears now and build a subscriber in both languages. We’ll start with PHP.
For this example, the subscriber file is stored as subscriber.php:
<?php /* * Class: Subscriber * Description: Provides ability to subscribe / unsubscribe from hub feeds */ class Subscriber{ private $regex_url = '|^https?://|i'; //simple URL string validator private $hub = ''; //hub URL private $callback = ''; //callback URL //constructor that stores the hub and callback URLs for the subscriber public function __construct($hub, $callback){ if (preg_match($this->regex_url, $hub)){ $this->hub = $hub; } else{ throw new Exception('Invalid hub URL supplied'); } if (preg_match($this->regex_url, $callback)){ $this->callback = $callback; } else{ throw new Exception('Invalid callback URL supplied'); } } //initiates a request to subscribe to a feed public function subscribe($feed){ return $this->change_subscription('subscribe', $feed); } //initiates a request to unsubscribe from a feed public function unsubscribe($feed){ return $this->change_subscription('unsubscribe', $feed); } //makes request to hub to subscribe / unsubscribe public function change_subscription($mode='subscribe', $feed){ //check if provided feed is a valid URL ...
No credit card required