Как использовать Superfeedr с PHP

Я работаю над проектом, который анализирует множество RSS-каналов, и я обнаружил, что у Superfeedr недостаточно документации о том, как использовать их API PubSubHubbub с PHP.

Кто-нибудь может дать мне хороший урок или пример, как использовать его для подписки на любой канал?

Спасибо,

4 ответа

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

API Superfeedr на самом деле является протоколом PubSubHubbub, поэтому я думаю, что первым шагом было бы найти хороший способ реализации PubSubHubbub. Здесь есть несколько ссылок, как эта, так и эта.

Я был там раньше. Вот вывод: 1. создайте файл PHP на вашем сервере и назовите его, например, endpoint.php, чтобы URL вашего файла был примерно таким: http://yoursite.com/endpoint.php

  1. Вы должны создать учетную запись на superfeedr.com, она должна дать вам user / pass
  2. Ваш PHP-файл должен делать две вещи, подписываться / отписываться от фидов, и в этом случае все, что вы должны писать (только) в своем файле, это hub_challenge

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

    После успешной подписки на ваши каналы вы должны (автоматически получать) новое содержание rss от суперпитателя. Используя PHP, вы должны получать содержимое, как это

    $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 
    }
    

Способ добавления ссылки rss очень прост, просто перейдите на superfeedr.com по ссылке на свой аккаунт в правом верхнем углу экрана, нажмите на нее и выберите панель управления.

нажмите xmpp, вы найдете список всех ваших каналов. Вы также можете добавить новый канал.

введите ссылку rss (http://example.com/rss.xml) и файл обратного вызова (endpoint.php), что-то вроде http://yoursite.com/endpoint.php

если вы хотите добавить его с помощью PHP-кода (в любом php-файле), выполните вызов curl с помощью запроса GET, как описано в документации.

  //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("youremail@yahoo.com","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:mypass@superfeedr.com/hubbub",$mypost);
return $result;
}

//then simply you can call
superfeedr_subscribe_rss("http://www.aljazeerasport.net/Services/Rss/?PostingId=201191510242078998");
Другие вопросы по тегам