aniss.bouraba
aniss.bouraba

Reputation: 453

How to use Superfeedr with PHP

I'm working on a project that parse a lot of RSS feeds, and I just discover Superfeedr it has a poor documentation in how to use their PubSubHubbub API with PHP.

Please can anyone give me a good tutorial or an example how to use it to subscribe to any feed?

Thank you,

Upvotes: 1

Views: 1337

Answers (4)

ashraf mohammed
ashraf mohammed

Reputation: 1350

  //first create file called callback.php
  //in this file write

  if(isset($_Get["hub_challenge"])){
       echo $_Get["hub_challenge"];
       return;
  }
  //which will be used to subscribe new rss feeds

  //second recieve data
  $my_array = json_decode(file_get_contents("php://input"));
  //offcource you will not be able to see that data and to test it do one of the following to test test results
  //1-mail it to your self
  mail("[email protected]","test callback",print_r($my_array,true)); 
  //2-write it to file and make sure that file has 0777 permissions
  file_put_contents("myfile.txt", print_r($my_array,true));

 //third step if you want to manually add rss links by code not with superfeedr console.
 //after inserting rss link to your db you have to send post request to superfeedr like this
 function superfeedr_curl_post($url, array $post = NULL, array $options = array()){
$defaults = array(  
    CURLOPT_POST => 1,
    CURLOPT_HEADER => 0,
    CURLOPT_URL => $url,
    CURLOPT_FRESH_CONNECT => 1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_FORBID_REUSE => 1,
    CURLOPT_TIMEOUT => 4,
    CURLOPT_POSTFIELDS => http_build_query($post)
);

$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if( ! $result = curl_exec($ch)){
    trigger_error(curl_error($ch));
}
curl_close($ch);
return $result;
 }

 function superfeedr_subscribe_rss($rss_url){
$mypost=array();
$mypost["hub.mode"]="subscribe";
$mypost["hub.verify"]="async";
$mypost["hub.callback"]="http://yoursite.com/yourcallback-file.php";
$mypost["hub.topic"]=$rss_url;
$mypost["superfeedr.digest"]=true;

$result=superfeedr_curl_post("http://myusername:[email protected]/hubbub",$mypost);
return $result;
}

//then simply you can call
superfeedr_subscribe_rss("http://www.aljazeerasport.net/Services/Rss/?PostingId=201191510242078998");

Upvotes: 0

Mohamed Hafez
Mohamed Hafez

Reputation: 51

I was there before. Here is the conclusion: 1. create a PHP file on your server and call it for example endpoint.php so the url of your file should be something like http://yoursite.com/endpoint.php

  1. You should create an account at superfeedr.com it should give you user/pass
  2. Your PHP file should do two things, subscribe/unsubscribe feeds and in this case all what you should write (only) in your file is the hub_challenge

    (if(isset($_Get["hub_challenge"])){ 
          echo $_Get["hub_challenge"];
        return;}//to ensure that it only echo the hub_challenge}
    

    After successfully subscribing your feeds you should (automatically recieve) new rss contents from superfeeder. Using PHP you should recieve contents like this

    $x=json_decode(file_get_contents("php://input"));
        $x now is an array of new contents.you should do what ever you want with this array.
    --the file endpoint should be like
    if(isset($_Get["hub_challenge"])){
       echo $_Get["hub_challenge"];return;
    }else{
        $x=json_decode(file_get_contents("php://input"));
        //then loop through it or what ever you want 
    }
    

The way you add rss link is very simple, just visit superfeedr.com on your account link in the top right of the screen click on it then select dashboard.

click xmpp you will find a list of all your feeds.you can also add new feed.

enter the rss link (http://example.com/rss.xml) and your callback (endpoint.php) file.something like http://yoursite.com/endpoint.php

if you want to add it by PHP code (in any php file).make a curl call with GET request as descibed in documentation.

Upvotes: 1

Mohamed Hafez
Mohamed Hafez

Reputation: 51

$x=json_decode(file_get_contents("php://input")); //for recieving new data.

Upvotes: 4

Julien Genestoux
Julien Genestoux

Reputation: 32982

Superfeedr's API is actually the PubSubHubbub protocol, so I guess the first step would be to find the good way to implement PubSubHubbub. There are a few links in here, like this one, or this one.

Upvotes: 1

Related Questions