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 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 ...

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