Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pubsubhubbub subscriber callback implementation in PHP

Tags:

php

websub

I am trying to use the PSHB protocol to be notified of my Google alerts. I am using the code found here. However, it is not clear to me how to implement the callback (or endpoint).

Can someone provide a trivial example that shows how I can access the data that has been POSTed to my endpoint?

A (slightly modified) snippet of the google code follows below:

<?php

// simple example for the PHP pubsubhubbub Subscriber
// as defined at http://code.google.com/p/pubsubhubbub/
// written by Josh Fraser | joshfraser.com | [email protected]
// Released under Apache License 2.0

include("subscriber.php");

$hub_url = "http://pubsubhubbub.appspot.com";
$callback_url = "http://www.example.com/mycallback.php"; // <- how to implement this ?

[[Edit]]

I have added some pseudocode below, to help clarify the question further ...

// Implementation of mycallback.php
<?php
    $pubsub_post_vars = $_POST[WHAT_NAME_AM_I_LOOKING_FOR]; //what's the name of the POST var?
    // How do I get to the 'good stuff?
    $feed_id    = $pubsub_post_vars[SOME_VARIABLE]
    $feed_title = $pubsub_post_vars[ANOTHER_VARIABLE]
    $contents   = $pubsub_post_vars[YET_ANOTHER_VARIABLE]
    $author     = $pubsub_post_vars[YET_ANOTHER_VARIABLE_1]
    $perma_link = $pubsub_post_vars[YET_ANOTHER_VARIABLE_2]
    $pub_date   = $pubsub_post_vars[YET_ANOTHER_VARIABLE_3]
?>

I realize that approach (above) may be complete wrong, as I suspect that it is an RSS/ATOM document that is POSTed. However, some skeleton code like the one above should be enough to get me started, so that I can extract things like the feed id, title and published content ... etc.

like image 848
Homunculus Reticulli Avatar asked Nov 05 '22 02:11

Homunculus Reticulli


1 Answers

Well, the way to implement it really depends on what you want to achieve with it. But generally, there are 2 things your callback needs to handle:

  • Verification of intent
  • Handling of notifications

For the Verification of Intent, your callback needs to echo the hub.challenge parameter, if you really want the subscription for that specific feed.

For the handling of the notification, your callback probably needs to check the validity (signature), if you'v used a secret when susbribing and, later it needs to read and save the content of the body.

[UPDATE] Beware, the notification will not be included in any POST variable, it will be the full body itself (Accessible thru $request_body = @file_get_contents('php://input');). POST vars are usually parsed by PHP from the body. In this context, you do want to access the raw body. You will then be able to extract all the vars you're looking from the XML (RSS or Atom) posted to you.

like image 64
Julien Genestoux Avatar answered Nov 07 '22 21:11

Julien Genestoux