Create a gist now

Instantly share code, notes, and snippets.

@barooney /composer.json Secret
Created Oct 26, 2015

Embed
What would you like to do?
Twitter Lists to RSS Feeds
{
"name": "barooney/twitter-list-rss",
"authors": [
{
"name": "Daniel Baron",
"email": "daniel@barooney.com"
}
],
"require": {
"j7mbo/twitter-api-php": "^1.0",
"suin/php-rss-writer": "^1.3"
}
}
<?php
require_once('../vendor/autoload.php');
use Suin\RSSWriter\Channel;
use Suin\RSSWriter\Feed;
use Suin\RSSWriter\Item;
$list = $_GET['list'];
$list = str_replace('https://twitter.com/', '', $list);
preg_match('/^(.+?)\/lists\/(.+?)$/i', $list, $matches);
if(3 !== count($matches)) {
return "This is not a valid Twitter List URL!";
}
$user = $matches[1];
$list = $matches[2];
$twitter = new TwitterAPIExchange([
'consumer_key' => 'YOUR_TWITTER_CONSUMER_KEY',
'consumer_secret' => 'YOUR_TWITTER_CONSUMER_SECRET',
'oauth_access_token' => 'YOUR_TWITTER_ACCESS_TOKEN',
'oauth_access_token_secret' => 'YOUR_TWITTER_TOKEN_SECRET',
]);
$info = json_decode(
$twitter
->setGetfield( '?slug=' . $list . '&owner_screen_name=' . $user )
->buildOauth( 'https://api.twitter.com/1.1/lists/list.json', 'GET' )
->performRequest()
)[0];
$statuses = json_decode(
$twitter
->setGetfield( '?slug=' . $list . '&owner_screen_name=' . $user )
->buildOauth( 'https://api.twitter.com/1.1/lists/statuses.json', 'GET' )
->performRequest()
);
$feed = new Feed();
$channel = new Channel();
$channel
->title($info->name)
->description($info->description)
->url('http://twitter.com' . $info->uri)
->appendTo($feed);
// RSS item
foreach ($statuses as $status) {
$status_url = 'https://twitter.com/statuses/' . $status->id_str;
$item = new Item();
$item
->title('Tweet by ' . $status->user->name . ' (@' . $status->user->screen_name . ')')
->description($status->text)
->guid($status_url)
->url($status_url)
->appendTo($channel);
}
header('Content-Type: application/rss+xml');
$feed_str = '' . $feed;
$feed_str = str_replace('<channel>', '<channel><atom:link href="' . $_SERVER['REQUEST_URI'] . '" rel="self" type="application/rss+xml" />', $feed_str);
echo $feed_str;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment