PHP code example of taproot / subscriptions

1. Go to this page and download the library: Download taproot/subscriptions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

taproot / subscriptions example snippets




// subscriptions.storage needs to be an instance implementing Taproot\Subscriptions\SubscriptionStorage
// Currently the only provided implementation is PdoSubscriptionStorage, which takes a PDO instance and an
// optional prefix
$app['subscriptions.storage'] = $app->share(function () use ($app) {
	return new Taproot\Subscriptions\PdoSubscriptionStorage($db, 'subscriptions_');
});

// subscriptions.defaulthub should be an subclass of Taproot\Subscriptions\PushHub, which will be used to 
// subscribe to content which doesn’t natively support PuSH. Most of the time this should be a Superfeedr hub,
// for which Taproot\Subscriptions\SuperfeedrHub is provided.
$app['subscriptions.defaulthub'] = function () use ($app) {
	return new Taproot\Subscriptions\SuperfeedrHub('username', 'password or token');
};



$app->mount('/subscriptions', Taproot\Subscriptions\controller($app, function ($request) {
	// For the subscription admin routes, check if the current user is allowed to view them.
	if (!userIsAdmin($request)) {
		$app->abort(401, 'Only admins may view this page');
	}
}, function ($resource) {
	// A new resource has been fetched, either via historical crawling or new content from a subscription.
	// Do something with the resource which has been fetched, e.g. store, index, processing
	$url = $resource['url'];
}));



use Taproot\Subscriptions;

list($subscription, $error) = Subscriptions\subscribe($app, 'http://waterpigs.co.uk');
if ($error !== null) {
	// There was a problem, and $error is a Guzzle Exception with more information about exactly what.
} else {
	// Otherwise, $subscription is an array representing the subscription which was just created
}



use Taproot\Subscriptions;

// The extra (optional) argument is a callback to be run for each page which gets crawled, in addition to any listeners
// on subscriptions.ping — handy e.g. for logging purposes
list($subscription, $error) = Subscriptions\subscribeAndCrawl($app, 'http://waterpigs.co.uk', function ($resource) {
	echo "Crawled {$resource['url']}\n";
});




function ($resource) {
	$posts = postsFromMicroformats($resource['mf']);
	foreach ($posts as $post) {
		createOrUpdatePost($post);
	}
}