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

Get Programming Social Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.